Solved

Change format of a date in java

Posted on 2013-11-06
8
212 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 75
use lov values 2 63
throw exception 21 58
How to determine if a string is a valid SHA value 7 35
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

827 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