Solved

change the format of a date field in JAVA

Posted on 2014-01-21
11
320 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
11 Comments
 
LVL 26

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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 35

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now