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

x
?
Solved

Change format of a date in java

Posted on 2013-11-06
8
Medium Priority
?
237 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
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.

 
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
 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

886 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