I am trying to parse a string to date, but the output look incorrect: Below is my code.
public static void main(String[] args){
Date startDate = new Date();
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");
try {
startDate = (Date) formatter.parse("07.10.2012 12:19:24");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Formatted Date " + startDate.toString());
}
Program Output:
Formatted Date Sun Oct 07 00:19:24 IST 2012
Expected Output:
Formatted Date Sun Oct 07 12:19:24 IST 2012
The parse() Method of SimpleDateFormat class is used to parse the text from a string to produce the Date. The method parses the text starting at the index given by a start position.
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.
2.2. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally. So SimpleDateFormat instances are not thread-safe, and we should use them carefully in concurrent environments.
Creating A Simple Date Format A SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. String pattern = "yyyy-MM-dd" ; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.
You might want to use 24h format instead of 12h format...
H Hour in day (0-23) Number 0
h Hour in am/pm (1-12) Number 12
Yes, formatting characters are case sensitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With