i2ijeya
i2ijeya

Reputation: 16410

date format in java

My requirement is to get the date in format MM/dd/yy. But I am currently getting the date value as "Sun Dec 31 00:00:00 IST 2006". I tried a sample code for the conversion as follows.

String pattern = "MM/dd/yyyy";
  SimpleDateFormat format = new SimpleDateFormat(pattern);
  try {
    Date date = format.parse("12/31/2006");
    System.out.println(date);
  } catch (ParseException e) {
    e.printStackTrace();
  }

Please help me to convert the given date into MM/dd/yy

Upvotes: 1

Views: 710

Answers (4)

Mike Yockey
Mike Yockey

Reputation: 4593

You need to use SDF (SimpleDateFormat) to process the output too.

String pattern = "MM/dd/yyyy"; 
  SimpleDateFormat format = new SimpleDateFormat(pattern); 
  try { 
    Date date = format.parse("12/31/2006"); 
    System.out.println(format.format(date)); 
  } catch (ParseException e) { 
    e.printStackTrace(); 
  }

Upvotes: 7

Buhake Sindi
Buhake Sindi

Reputation: 89169

The reason of your output is because you're outputting the date object through System.out.println(date); which is effectively, translated to System.out.println(date.toString());

The toString() method of Date outputs date in the format of:

EEE MMM dd HH:mm:ss zzz yyyy

Here's the code for Date.toString()

public String toString() {
    // "EEE MMM dd HH:mm:ss zzz yyyy";
    BaseCalendar.Date date = normalize();
    StringBuilder sb = new StringBuilder(28);
    int index = date.getDayOfWeek();
    if (index == gcal.SUNDAY) {
        index = 8;
    }
    convertToAbbr(sb, wtb[index]).append(' ');            // EEE
    convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' ');  // MMM
    CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd

    CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':');   // HH
    CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
    CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
    TimeZone zi = date.getZone();
    if (zi != null) {
        sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz
    } else {
        sb.append("GMT");
    }
    sb.append(' ').append(date.getYear());  // yyyy
    return sb.toString();
    }

Your code is correct though. Use SimpleDateFormat to display the date like so:

System.out.println(format.format(date));

Upvotes: 1

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

Change your code to:

String pattern = ;
SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/yy");
try {
    Date date = inputFormat.parse("12/31/2006");
    System.out.println(outputFormat.format(date));
 } catch (ParseException e) {
    e.printStackTrace();
 }

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500525

You're using the SimpleDateFormat to parse a string, and that's working fine - but then you're using Date's toString method (implicitly) when formatting the date. That will use a default format which is completely independent of the format which was originally used to parse the value.

A Date object knows nothing about how you want to format it. That's what you should be using SimpleDateFormat for.

You can use SimpleDateFormat to format it again:

System.out.println(format.format(date));

... but a better approach would be to switch to Joda Time and use its DateTimeFormatter class, which is thread-safe and immutable, unlike SimpleDateFormat... the rest of its API is better, too.

Upvotes: 0

Related Questions