• 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

getting the correct Date problem

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be careful of the format pattern. "mm" means minutes, not month.
Use "MM" instead.
 
Ranch Hand
Posts: 63
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you friends. Now i am getting correct result.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 63
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi pals
my apologies for the mistake.
jesper, thanks for the correction...
cheers
vinny m
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic