12

I am getting date into string in YYYY/MM/DD HH:MM:SS format.I want to change it into the mm/dd/yyyy HH:mm:ss and also it will show AM and PM how can I do this.please help me

Thank you

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
user1061793
  • 1,047
  • 8
  • 19
  • 27

6 Answers6

34

To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.

Note: HH is for 24 hour and hh is for 12 hour date format

SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
            String newFormat = formatter.format(testDate);

Example

String date = "2011/11/12 16:05:06";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
        Date testDate = null;
        try {
            testDate = sdf.parse(date);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
        String newFormat = formatter.format(testDate);
        System.out.println(".....Date..."+newFormat);
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
10

You can use the SimpleDateFormat for the same kinds of any date operations.

SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
SimpleDateFormat DesiredFormat = new SimpleDateFormat("MM/dd/yyyy HH:MM:SS a");   
                                                             // 'a' for AM/PM

Date date = sourceFormat.parse("2012/12/31 03:20:20");
String formattedDate = DesiredFormat.format(date.getTime());  
// Now formattedDate have current date/time  
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();  
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
1

Use android.text.format.Time to the conversion. You can pass the time as text and it will return you the time in desired format by using timeInstance.format("");

you need to provide formatter.

Refer to following: Time : http://developer.android.com/reference/android/text/format/Time.html Formatter: http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html

you can use any combination of formatter string to work with it :)

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
Abhinava
  • 1,030
  • 9
  • 19
1

just use the Time class. Try something similar to this.

Time time = new Time();
time.set(Long.valueOf(yourTimeString));

If you really need a Date object just try this.

Date date = new Date(Long.parse(yourTimeString));
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0
public static String getFormatedDate(String strDate, String sourceFormate,
        String destinyFormate) {
    SimpleDateFormat df;
    df = new SimpleDateFormat(sourceFormate);
    Date date = null;
    try {
        date = df.parse(strDate);

    } catch (ParseException e) {
        e.printStackTrace();
    }

    df = new SimpleDateFormat(destinyFormate);
    return df.format(date);

}

and use like

getFormatedDate(strDate,
            "yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy") 
0

you can use SimpleDateFormat or DateFormat for this here is the example

SimpleDateFormat gsdf = new SimpleDateFormat("YYYY/MM/DD HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
try{
    Date d = gsdf.parse("2011/12/13 16:17:00");
    System.out.println("new date format " + sdf.format(d));
}catch(Exception){
     // handle exception if parse time or another by cause
}
Pratik
  • 30,639
  • 18
  • 84
  • 159