Solved

change the format of a date field in JAVA

Posted on 2014-01-21
11
330 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 50 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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 125 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 25 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

617 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