• 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

Date Format

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a string for date pattern called dPattern which is of the form ""mm/dd/yyyy"(dPattern = ""mm/dd/yyyy").I use this pattern to make the SimpleDateFormat class as
SimpleDateFormat sd = new SimpleDateFormat(dPattern);
I take a String of the form ,say ex: ateStr = "09/12/2001";
Date dt = sd.parse(dateStr, new ParsePosition(0));
The date dt when turned as a String gives me a strange Date as Jan 2001.
Also, if I use it in the Gregorian Calendar to set the time and access the month, the month comes out as January.
Can you pl explain me the reason and also, tell me how to do it right??
Here is the total code::
dPattern = ""mm/dd/yyyy"
SimpleDateFormat sd = new SimpleDateFormat(dPattern);
Date dt = sd.parse(dateStr, new ParsePosition(0));
System.out.println(dt.toString());

Calendar calendar = new GregorianCalendar();
calendar.setTime(dt);
int month = calendar.get(Calendar.MONTH) + 1;

Thanks in advance,
Madhu
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Look at the API for SimpleDateFormat.
For some reasons, sun decide "m" for Minute and "M" for Month.
So, the pattern should be "MM/dd/yyyy".
And if you take a close look of your code, the output, you will see this
QUOTE]
Mon Jan 28 00:09:00 EST 2002
---------------^
9 as 9-minute

kawaii
[ February 25, 2002: Message edited by: kawaii desu ]
 
The City calls upon her steadfast protectors. Now for a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic