• 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 Problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 62
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Swapna Agarwal
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if i change it to MM/DD/yyyy, its giving same output
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot, i got it. If u have any example code for my requirement,please let me know
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic