Reputation: 2444
I have a string holding a Date and time I want to be able to strip out the AM - and the PM and convert all the times to 24 hour format so 9:30PM would really be 21:30 .
Im having this code which im trying to convert into specific formate but cant get the proper result
String DateTime="14/03/2012 12:32:38 PM";
format=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
try {
date=format1.parse(TransactionDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
After that im trying to convert date in yyyy-MM-dd and time in 24 Hour format but im not getting proper result.
format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TransDateTime=format1.format(date);
im getting result is 2012-03-14 12:32:38 PM but i want time in 24 hour formate date is correct but time not in 24 hour format .
Sorry this is commom question but i try all the solutions and result is same can any buddy tell me whats the problem .
Upvotes: 1
Views: 6387
Reputation: 2762
Please use following code, it will solve your problem,
public static void main(String[] args) {
// Make a String that has a date in it, with MEDIUM date format
// and SHORT time format.
String dateString = "Nov 4, 2003 8:14 PM";
// Get the default MEDIUM/SHORT DateFormat
DateFormat format =
DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.SHORT);
// Parse the date
try {
Date date = format.parse(dateString);
System.out.println("Original string: " + dateString);
System.out.println("Parsed date : " +
date.toString());
}
catch(ParseException pe) {
System.out.println("ERROR: could not parse date in string \"" +
dateString + "\"");
}
}
Upvotes: 1
Reputation: 157437
to get the hour in 24 hours format you have to use
" H:mm";
as pattern
Upvotes: 0