Solved

Change format of a date in java

Posted on 2013-11-06
8
218 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
[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
  • 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
Technology Partners: 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!

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 83
Notify sent to other threads in Java 9 54
Bot application - advice 3 81
restrict decimal places for double datatype 10 49
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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

751 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