-3

I have date like Tue Mar 19 00:41:00 GMT 2013, how to convert it to 2013-03-19 06:13:00?

final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date date = bdate; 
Date ndate = formatter.parse(formatter.format(date)); 
System.out.println(ndate);

gives the same date.

good_evening
  • 21,085
  • 65
  • 193
  • 298

5 Answers5

4

Use two SimpleDateFormat objects with appropriate formats and use the first to parse the string into a date and the second to format the date into a string again.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
2

As the first answer says. First parse your date with SimpleDateFormat like this:

Date from = new SimpleDateFormat("E M d hh:mm:ss z yyyy").parse("Tue Mar 19 00:41:00 GMT 2013");

Then use that to format the resulting date object with another instance of SimpleDateFormat like this:

String to = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(from);

See the javadoc of SimpleDateFormat here. Hope that helps.

Sten Roger Sandvik
  • 2,526
  • 2
  • 19
  • 16
2

One major thing that the others have left out is dealing with the timezone (TZ). Anytime you use a SimpleDateFormat to go to/from a string representation of the date, you really need to be aware of what TZ you're dealing with. Unless you explicitly set the TZ on the SimpleDateFormat, it will use the default TZ when formatting/parsing. Unless you only deal with date strings in the default timezone, you'll run into problems.

Your input date is representing a date in GMT. Assuming that you also want the output to be formatted as GMT, you need to make sure to set the TZ on the SimpleDateFormat:

public static void main(String[] args) throws Exception
{
    String inputDate = "Tue Mar 19 00:41:00 GMT 2013";
    // Initialize with format of input
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    // Configure the TZ on the date formatter. Not sure why it doesn't get set
    // automatically when parsing the date since the input includes the TZ name,
    // but it doesn't. One of many reasons to use Joda instead
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = sdf.parse(inputDate);
    // re-initialize the pattern with format of desired output. Alternatively,
    // you could use a new SimpleDateFormat instance as long as you set the TZ
    // correctly
    sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
    System.out.println(sdf.format(date));
}
Blake
  • 581
  • 4
  • 14
1

Use SimpleDateFormat in this way:

final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date date = new Date();
System.out.println(formatter.format(date));
Igor Rodriguez
  • 1,196
  • 11
  • 16
  • How to convert formatter.format(date) to date? – good_evening Mar 19 '13 at 20:14
  • @evening: with formatter.parse(String), giving it a string in the same format. – Igor Rodriguez Mar 19 '13 at 20:17
  • final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); final Date date = bdate; Date ndate = formatter.parse(formatter.format(date)); System.out.println(ndate); – good_evening Mar 19 '13 at 20:18
  • @evening: Yes, they are inverse operations. – Igor Rodriguez Mar 19 '13 at 20:23
  • @evening: The date you are printing in your last comment is not formatted, that's the reason you get the default output for date: "Tue Mar 19 00:41:00 GMT 2013". For displaying it in other way, you need to format, as posted. – Igor Rodriguez Mar 19 '13 at 20:25
  • 1
    @evening: Date class stores the date as a long value, but when you print it, it displays it in a readable format, that is: "Tue Mar 19 00:41:00 GMT 2013". You don't need to do anything with that format until you decide to display it to the user. In that moment need to format that date to the way you would like to display it, and you do it with the SimpleDateFormat, that returns a String in the format you specified, in your case "2013-03-19 06:13:00" (using the posted pattern). – Igor Rodriguez Mar 19 '13 at 20:29
0

If you do any calculations or parsing with dates please use JodaTime because the standard JAVA date support is really buggy

emd
  • 740
  • 4
  • 12