Solved

Change format of a date in java

Posted on 2013-11-06
8
202 Views
Last Modified: 2013-11-10
How do I convert this date -> Oct 16, 2013 10:18:38 AM to YYYY-MM-DD HH:mm:ss
0
Comment
Question by:SiemensSEN
  • 4
  • 2
  • 2
8 Comments
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39628049
final String OLD_FORMAT = "MMM dd, yyyy";
final String NEW_FORMAT = "yyyy/mm/dd";


String oldDateString = "Oct 16, 2013";
String newDateString;

SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39628146
How do I convert this date
Do you have that as type Date or type String?
0
 

Author Comment

by:SiemensSEN
ID: 39629046
I am casting it to a date

(Date)sd.getFieldValue("timestamp");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39629059
Date d = (Date)sd.getFieldValue("timestamp");
String isoTimestamp = new java.sql.Timestamp(d.getTime()).toString();

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:SiemensSEN
ID: 39629087
Thank you
This is what i see
2013-10-16 15:09:49.604

Is it possible to show me how to convert it  back to this fotmat 03-10-02T19:13:35Z
0
 
LVL 9

Accepted Solution

by:
QuinnDex earned 200 total points
ID: 39629398
same thing new format

final String OLD_FORMAT = "yyyy-MM-dd HH:mm:ss";
final String NEW_FORMAT = "dd-MM-yyyy HH:mm:ss";


String oldDateString = "2013-10-16 15:09:49.604";
String newDateString;

SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);

Open in new window

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 39629637
Thank you
This is what i see
2013-10-16 15:09:49.604

(which is what you asked for [though it does have milliseconds too]):
YYYY-MM-DD HH:mm:ss 

Open in new window


Is it possible to show me how to convert it  back to this fotmat 03-10-02T19:13:35Z
Yes, you can use

new SimpleDateFormat("dd-MM-yy'T'hh:mm:ss'Z'");

Open in new window

or use the 'write-only language' of printf style formatting:
String s = String.format("%Td-%<Tm-%<TyT%<TTZ", d);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39637076
final String NEW_FORMAT = "dd-MM-yyyy HH:mm:ss";

Open in new window

Will not give you what you asked for btw
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

896 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

18 Experts available now in Live!

Get 1:1 Help Now