Solved

Change format of a date in java

Posted on 2013-11-06
8
199 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
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 …

706 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

20 Experts available now in Live!

Get 1:1 Help Now