Ashr Raza
Ashr Raza

Reputation: 41

Java DateFormat in Date Object

There is a simple problem of Date Format Conversion. In java when we print date object its in this format "Tue Nov 13 18:34:35 PKT 2012" But I want this date to be shown in this format "2012-11-13 18:34:35" with in Date object. No string is required. Only Date object is needed in above format. Can any one help me. I am using

Date d = new Date();  
SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
Date formattedDate = sdf.parse(d.format(sdf)); 

Upvotes: 2

Views: 2255

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 340350

tl;dr

ZonedDateTime
    .parse( 
        "Tue Nov 13 18:34:35 PKT 2012" , 
        DateTimeFormatter
            .ofPattern( "EEE MMM dd HH:mm:ss z uuuu" )
            .withLocale( locale) ;
    )
    .toString()  // Generate text in standard ISO 8601 format wisely extended by appending the name of the time zone in square brackets.

2012-11-13T18:34:35+05:00[Asia/Karachi]

Poor choice of format

First of all, that is a terrible format for exchanging date-time values. Problems include:

  • Text is localized. Better to avoid using name of day of week, name of month, etc.
  • Neglects to include an offset.
  • Uses 2-4 letter pseudo time zone such as PKT. These pseudo-zones are not standardized. They are not even unique! Use only for presentation to the user.

I strongly advise you to educate the publisher of your incoming data to use only standard ISO 8601 formats for data-exchange. Ditto for logging, data-storage, etc. Localization should only be used for presentation to a human.

Avoid legacy date-time classes

Never use Date, Calendar, SimpleDateFormat, etc. These terribly-flawed legacy classes were supplanted in Java 8+ by the modern java.time classes defined in JSR 310.

java.time

Define a formatting pattern to match your input. Use DateTimeFormatter class.

DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" );

But remember, those 2-4 character pseudo-zones are neither standardized nor unique, so this particular pattern may not succeed with all possible inputs. Instead, use real time zones in name of Continent/Region such as Asia/Karachi.

Parse into a ZonedDateTime object. This represents a moment, a point on the timeline, as seen in a particular time zone.

Specify the human language and cultural norms to be used in localizing. Pass a Locale object.

String input = "Tue Nov 13 18:34:35 PKT 2012"  ;
Locale locale = new Locale( "en" , "US" ) ;
DateTimeFormatter f = 
    DateTimeFormatter
        .ofPattern( "EEE MMM dd HH:mm:ss z uuuu" )
        .withLocale( locale) ;
ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;

See this code run at Ideone.com.

2012-11-13T18:34:35+05:00[Asia/Karachi]

Upvotes: 0

kosa
kosa

Reputation: 66677

I think you are confused. Formatting is only for display purpose, not for computation purpose. Date() object doesn't represent format, It is number of milliseconds of epoch time. Using SimpleDateFormat class format() method you can format the date and print as you wish.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1504182

But I want this date to be shown in this format "2012-11-13 18:34:35" with in Date object. No string is required.

The format you talk about is a string. If no string is required, then your first requirement goes away completely. You want the date to be shown, which means a string is required.

A Date has no concept of a format. It's just a number of milliseconds since the Unix epoch. There's no information within Date which controls the format.

Just use SimpleDateFormat, adjusting it to whatever format - and calendar, and time zone, and locale - you want. Don't waste time trying to change the Date itself, because you won't get anywhere. Treat Date.toString() as a diagnostic convenience and nothing more.

Upvotes: 4

Related Questions