• Post Reply Bookmark Topic Watch Topic
  • New Topic

String to Date Conversion Problem  RSS feed

 
S.Reddisekhar Reddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Team,
I am facing the Problem while converting string to date
I am getting same output for the two lines 6 and 7 which is wrong , in the below program

Program::
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateManipulation {
public static void main(String[] args) {
// String str_date="25/05/2013";
String str_date="25/11/2013";
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("dd/mm/yyyy");
try {
format.setLenient(false);
Date date = format.parse(str_date);
System.out.println("ConvertedDate::"+date);
} catch (ParseException e) {
System.out.println("Invalid Date FOrmat");
}
}
}

Output::
ConvertedDate::Fri Jan 25 00:11:00 IST 2013

Expected::
Nov instead of Jan and May instead of May

Can you please suggest on this
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go back and use the button to add code tags, so we can actually read the code.
Go through the Java Tutorials, and the date format class and remind yourself what the letters mean, particularly ‘m’.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell has already provided you direction. Checkout the format. M is case sensitive since it can also refer to minutes.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to re-read the SimpleDateFormat Javadoc paying particular attention to what defines months and what defines minutes !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!