?
Solved

change the format of a date field in JAVA

Posted on 2014-01-21
11
Medium Priority
?
333 Views
Last Modified: 2014-01-28
How do I convert a date field [Jan 17, 2014 5:57:05 PM] to  yyyy-mm-dd HH::MM::ss

--My Definition--
Date timestamp;

--get the value--
this.timestamp= (Date)sd.getFieldValue("timestamp");


Thanks for your help
0
Comment
Question by:SiemensSEN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 200 total points
ID: 39797650
Are you getting this field from a database?

If so what you usually want is java.sql.Timestamp:

Timestamp timestamp = (Timestamp).sd.getFieldValue("timestamp") ;

Date javaDate = new Date(timestamp.getTime()) ;

Then if you wish to format it in a specific way (like yyyy-mm etc.) use the SimpleDateFormat class.

DATE_FORMAT = new SimpleDateFormat("yyyy-mm-dd HH::MM::ss");
String stringDate = DATE_FORMAT.format(javaDate);

Doug
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39797728
to  yyyy-mm-dd HH::MM::ss
If you have a variable of type java.sql.Timestamp, its toString method will give you that format
0
 

Author Comment

by:SiemensSEN
ID: 39797758
I am reading the date information from SOLR database. The date field in define as a 'Date' type
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SiemensSEN
ID: 39797767
I am reading the date information from SOLR database. The date field in SOLR define as a 'Date' type  and I need to return a variable of type date with format(yyyy-mm-dd HH:MM:SS)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39797888
If the type is Date ('d') then all you need do is
String timestamp = new java.sql.Timestamp(d.getTime()).toString();

Open in new window

0
 

Author Comment

by:SiemensSEN
ID: 39797903
Sorry ...

The value is I get from SOLR is of type DATE e.g Jan 17, 2014 5:57:05 PM
I would like to convert it to format (yyyy-mm-dd HH:MM:ss) and return this value as type 'Date'

Thanks for your help . I will try to debug with your suggession
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39797945
The value is I get from SOLR is of type DATE
This is the Java TA. Which Java type do you get?
0
 

Author Comment

by:SiemensSEN
ID: 39797953
Java.util.Date
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39798040
OK good. See my previous code example
0
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 100 total points
ID: 39802832
I would like to convert it to format (yyyy-mm-dd HH:MM:ss) and return this value as type 'Date'
The type java.util.Date has no 'format' other than its built in toString() method. You can't change that. But that doesn't matter; you just pass the Date value back and the code that receives it formats it as required (as shown by CEHJ's code)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39815265
:)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question