Solved

change the format of a date field in JAVA

Posted on 2014-01-21
11
327 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 27

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 125
login form jsp example 2 50
Problem to Alipay 10 70
running on tomcat not jboss eap 7.0 3 30
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 “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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

756 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