• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting the correct Date problem  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends i hav a date in string variable . i want one day extra to that date.For that i m using java.util.Date . But i am not getting correct month. Here is my sample code.

public static void main(String[] args) {

String to="23/05/2006";
System.out.println("input:"+to);
SimpleDateFormat dateformat=new SimpleDateFormat("dd/mm/yyyy");
String to_str=null;
try{
java.util.Date todate=dateformat.parse(to);
System.out.println("Date:"+todate);

//long msec=todate.getTime()+24*60*60*1000;

}catch(Exception e){
e.printStackTrace();
}

}



Here that print statement giving jan 23 2006 But i hav given May 23.
Even for any month it is giving jan only.
wat is the reason.
anyone help me
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful of the format pattern. "mm" means minutes, not month.
Use "MM" instead.
 
Ranch Hand
Posts: 62
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jakeer,
try this:
import

and use gregoriancalendar to do the needful:


Also,if u give the Date format as


The mm stands for minutes and MM stands for months in the notation.
refer the sun site for Dateformat documentation.

hth,
cheers
vinny m
 
jakeer ahmed
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you friends. Now i am getting correct result.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinny, this is incorrect:

First of all, month numbers in Java are zero-based (0 = January, 1 = February, ..., 11 = December). So month 5 is April, not May, as you would expect.

Second, integer literals that start with a zero (05 in your code above) are interpreted as octal values, not decimal. For 05 it doesn't matter, but if you'd type 08 for example, the code would not even compile.
 
Vinny Menon
Ranch Hand
Posts: 62
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi pals
my apologies for the mistake.
jesper, thanks for the correction...
cheers
vinny m
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!