• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Date Format

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Iam converting the date to MM/dd/yyyy format and converting it back to date and iam getting starnge results. Can someone correct where iam doing wrong?

Code snippet is below. Iam taking today's date: 04/16/2008

Calendar d1 = new GregorianCalendar();
System.out.println("Date is "+ d1.getTime());

DateFormat df =DateFormat.getDateInstance(DateFormat.SHORT);
String str1 = df.format(d1.getTime());
System.out.println("SHORT Style = " + str1);

==> Date is Wed Apr 16 10:44:50 EDT 2008
==> SHORT Style = 4/16/08

// now iam converting the date again from String to Date.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date d111 = sdf.parse(str1, new ParsePosition(0));
System.out.println("d111 = "+d111);

==> d111 = Mon Apr 16 00:00:00 EDT 0008

Its printing "Mon". what is wrong with the code?

Thanks,
Raj Chukka
 
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its also printing year 0008 (rather than 2008). The short date format does not include the century (i.e. 2000 or 1900) and when you make it make into a date it assumes the century is 0.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try use SimpleDateFormat with the same date pattern in both format and parse method.
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic