how to convert date and time to 12 hour format

14,032

Solution 1

You need two formats: one to parse, and one to format. You need to parse from String to Date with one DateFormat, then format that Date into a String with the other format.

Currently, your single SimpleDateFormat is half way between - you've got HH which is 24-hour, but you've also got aa which is for am/pm. You want HH without the aa for input, and hh with the aa for output. (It's almost never appropriate to have both HH and aa.)

TimeZone utc = TimeZone.getTimeZone("etc/UTC");
DateFormat inputFormat = new SimpleDateFormat("dd MMM, yyyy HH:mm",
                                              Locale.US);
inputFormat.setTimeZone(utc);
DateFormat outputFormat = new SimpleDateFormat("dd MMM, yyyy hh:mm aa",
                                              Locale.US);
outputFormat.setTimeZone(utc);

Date date = inputFormat.parse(input);
String output = outputFormat.format(date);

Note that I'm setting the locale to US so it can always parse "Nov", and the time zone to UTC so you don't need to worry about certain times being skipped or ambiguous.

Solution 2

try with this answer this is shortest and best answer on stack.

    Calendar c = Calendar.getInstance();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");
     Datetime = sdf.format(c.getTime());
     System.out.println("============="+Datetime);

Result:-=========2015-11-20 05:52:25 PM

try with this link Link here

Solution 3

Try understand SimpleDateFormat Symbol and Meaning :

 Symbol       Meaning
   H      hour in day (0-23)
   K      hour in am/pm (0-11)
   h      hour in am/pm (1-12)
   k      hour in day (1-24)

SimpleDateFormat  dateformat = new SimpleDateFormat("dd' 'MMM,' 'yyyy KK:mm aa");

OR

SimpleDateFormat  dateformat = new SimpleDateFormat("dd' 'MMM,' 'yyyy hh:mm aa");

Example:

convertDateStringFormat("12 Nov, 2014 23:13","dd MMM, yyyy HH:mm","dd MMM, yyyy hh:mm aa")

public String convertDateStringFormat(String strDate, String fromFormat, String toFormat){
   try{
       SimpleDateFormat sdf = new SimpleDateFormat(fromFormat);
       sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
       SimpleDateFormat dateFormat2 = new SimpleDateFormat(toFormat.trim());
       return dateFormat2.format(sdf.parse(strDate));
   }catch (Exception e) {
       e.printStackTrace();
       return "";
   }
 }
Share:
14,032
Ahsan Malik
Author by

Ahsan Malik

Updated on July 08, 2022

Comments

  • Ahsan Malik
    Ahsan Malik almost 2 years

    hello i have a date and time string in 24 hour format but i want it in 12 hour how i can do this my string is

    String date_st="12 Nov, 2014 23:13"
    

    i am doing this

     SimpleDateFormat  dateformat = new SimpleDateFormat("dd' 'MMM,' 'yyyy HH:mm aa");
    

    but its not converting according to my need it shows 23:13 PM

    i want to show in following format

    String con_date= "12 Nov, 2014 11:13 PM"
    

    how i can do this?

  • Jon Skeet
    Jon Skeet over 9 years
    @AhsanMalik: Do you mean the one where I wasn't setting the locale or time zone? If so, it will work if you're in the right locale and if the given date/time isn't ambiguous/skipped in the default time zone. I prefer to give answers which will work for all valid input though :) Of course, if your input actually uses the month names from the current locale, you shouldn't specify the locale... but in my experience, most of the time that you're parsing a text representation which includes a month name, it is in English even if the user isn't.
  • ajitksharma
    ajitksharma over 9 years
    @JonSkeet: nice answer, well explained...no other answer required here...great...+1
  • Muhammad Babar
    Muhammad Babar over 9 years
    Note that I'm setting the locale to US so it can always parse "Nov", and the time zone to UTC so you don't need to worry about certain times being skipped or ambiguous. Jon can you please explain this? Wouldn't the month will parse to Nov if i use chinese or lets say gb locale? and what's this times being skipped or ambigous?
  • Jon Skeet
    Jon Skeet over 9 years
    @MuhammadBabar: It would work for the GB, but I doubt that it would work in a Chinese locale, as I doubt that the Chinese short name for November is "Nov". The part about times being skipped or ambiguous is due to time zones which skip over or repeat times due to daylight saving transitions.
  • Muhammad Babar
    Muhammad Babar over 9 years
    @JonSkeet well that's a new thing for me. Thanks