| Author |
DATE Problem
|
Swapna Agarwal
Ranch Hand
Joined: Jan 30, 2004
Posts: 37
|
|
Why this code is diplaying wrong parsing !!! I am trying to parse the date which is given by user on command line. If i give 03/24/2004 its displying "Sat Jan 24 00:03:00 EST 2004" which is wrong. This is my Code public class DateFormatExample3 { public static void main(String[] args) { String sDateFormatter = "mm/dd/yyyy"; java.text.SimpleDateFormat format = new SimpleDateFormat(sDateFormatter); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { System.out.print("ENTER DATE STRING: "); String dateString = reader.readLine(); while ((dateString != null) && (dateString.length() > 0)) { try { Date date = format.parse(dateString); System.out.println("Original string: " + dateString); System.out.println("Parsed date : " + date.toString()); System.out.println(); // Skip a line } catch(ParseException pe) { System.out.println( "ERROR: could not parse date in string \"" + dateString + "\""); } System.out.print("ENTER DATE STRING: "); dateString = reader.readLine(); } } catch(IOException ioe) { System.out.println("I/O Exception: " + ioe); } } } My intension is by taking two input dates from user, my method should give an array of DATE object which are all SUNDAYS between these 2 dates. example: INPUT: startDate=03/24/2004 endDate=02/24/2004 OUTPUT: 03/21/2004 03/14/2004 03/07/2004 02/29/2004 Could any one pose some light on this, i need it very urgently Thanks swapna
|
 |
Vilpesh Mistry
Ranch Hand
Joined: May 27, 2003
Posts: 60
|
|
hi well iam not java champ and may be what i suggest is wrong,please bear well after going through your code i don't find that the date taken is according to the correct Timezone or correct Locale. their is no code setting the date according to indian Timezone and Locale..please look into the above mentioned class bye
|
Thanks.
|
 |
Kaustubh Patil
Ranch Hand
Joined: Aug 13, 2001
Posts: 164
|
|
Swapna, The formatting string is not correct. you must indicate month by "MM" and not by "mm". "mm" indicates minutes. Try this out. Atleast it should parse the date correctly. -Kaustubh. [ March 24, 2004: Message edited by: Kaustubh Patil ]
|
Kaustubh. Mumbai, India.
|
 |
Swapna Agarwal
Ranch Hand
Joined: Jan 30, 2004
Posts: 37
|
|
|
Even if i change it to MM/DD/yyyy, its giving same output
|
 |
Kaustubh Patil
Ranch Hand
Joined: Aug 13, 2001
Posts: 164
|
|
I just mentioned for the month. the day format string is correct. make the format string as "MM/dd/yyyy". [ March 24, 2004: Message edited by: Kaustubh Patil ]
|
 |
Swapna Agarwal
Ranch Hand
Joined: Jan 30, 2004
Posts: 37
|
|
|
Thanks alot, i got it. If u have any example code for my requirement,please let me know
|
 |
Kaustubh Patil
Ranch Hand
Joined: Aug 13, 2001
Posts: 164
|
|
I can give you hints. Find out how far is the sunday from start date. So if your start date falls on Wed. there are still 4 days to sunday (start date included). deduct this number from the total days. Similarly find out how many days have passed since last sunday w.r.t. the end date. So if your end date is on tue. 3 days have passed since last sunday (sunday included). deduct these many days again from the total days. Dividing the remaining days by 7 will give you number of weeks and also the number of sundays. Add one sunday to it (to compensate for the sunday you deducted w.r.t. end date). Also you have to take care of the boundary conditions like if start/end dates fall on sundays etc.etc. There may be some corrections you hv to do in this algo. but i guess you got a fair idea. Hope this helps. Kaustubh
|
 |
Kaustubh Patil
Ranch Hand
Joined: Aug 13, 2001
Posts: 164
|
|
Also, Once you get the first sunday you will have to add the logic to see if the next sunday crosses in to the next month. I am too tired and lazy to think a logic for it. Kaustubh.
|
 |
 |
|
|
subject: DATE Problem
|
|
|