Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

change the format of a date field in JAVA

Posted on 2014-01-21
11
Medium Priority
?
336 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

877 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