91

The time I get from the server is like Jul 27, 2011 8:35:29 AM.

I want to convert it to yyyy-MM-dd HH:mm:ss.

I also want the converted time to be in 24 hour format. Can any one give solution to this problem. The output I want to get is like 2011-07-27 08:35:29

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
Sujiz
  • 1,720
  • 1
  • 20
  • 30

12 Answers12

209

H vs h is difference between 24 hour vs 12 hour format.

Jackie
  • 25,199
  • 6
  • 33
  • 24
123

Try this:

String dateStr = "Jul 27, 2011 8:35:29 AM";
DateFormat readFormat = new SimpleDateFormat( "MMM dd, yyyy hh:mm:ss aa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
    date = readFormat.parse(dateStr);
} catch (ParseException e) {
    e.printStackTrace();
}

if (date != null) {
    String formattedDate = writeFormat.format(date);
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • 5
    I am not able to convert 2:46 PM to 14:46 with this code. Am I missing something? – pnv Jul 28 '15 at 16:56
  • @user1930402 you can do this by changing your read format to `hh:mm a` - your input ignores the seconds (is a guess). – Dhruv Ghulati Jun 06 '16 at 22:37
  • 1
    when we have aa in the format makesure that hh is small case else it should be in capitol – Narsireddy Apr 04 '18 at 14:34
  • 3
    Very small and hard to identify if you are not aware with pattern of date format. For me, it is just a change of `hh` to `HH` and problem solved! – S_K Jun 06 '19 at 11:08
105
kk = Hours in 1-24 format
hh= hours in 1-12 format
KK= hours in 0-11 format
HH= hours in 0-23 format
Savas Adar
  • 4,083
  • 3
  • 46
  • 54
11

Try this:

Date date=new Date("12/12/11 8:22:09 PM");    
System.out.println("Time in 24Hours ="+new SimpleDateFormat("HH:mm").format(date));
Yannick Blondeau
  • 9,465
  • 8
  • 52
  • 74
Jaadu
  • 111
  • 1
  • 2
5

You should take a look at Java date formatting, in particular SimpleDateFormat. There's some examples here: http://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html - but you can also find a lot more with a quick google.

stephendnicholas
  • 1,810
  • 14
  • 15
4

I am taking an example of date given below and print two different format of dates according to your requirements.

String date="01/10/2014 05:54:00 PM";

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa",Locale.getDefault());

try {
            Log.i("",""+new SimpleDateFormat("ddMMyyyyHHmmss",Locale.getDefault()).format(simpleDateFormat.parse(date)));

            Log.i("",""+new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",Locale.getDefault()).format(simpleDateFormat.parse(date)));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

If you still have any query, Please respond. Thanks.

droidd
  • 1,361
  • 10
  • 15
3
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Date date = new Date();
    Date date2 = new Date("2014/08/06 15:59:48");

    String currentDate = dateFormat.format(date).toString();
    String anyDate = dateFormat.format(date2).toString();

    System.out.println(currentDate);
    System.out.println(anyDate);
Anahit Serobyan
  • 432
  • 1
  • 6
  • 17
2
import java.text.SimpleDateFormat;

import java.util.Date;

public class DateFormatExample {

public static void main(String args[]) {

    // This is how to get today's date in Java
    Date today = new Date();

    //If you print Date, you will get un formatted output
    System.out.println("Today is : " + today);

    //formatting date in Java using SimpleDateFormat
    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
    String date = DATE_FORMAT.format(today);
    System.out.println("Today in dd-MM-yyyy format : " + date);

    //Another Example of formatting Date in Java using SimpleDateFormat
    DATE_FORMAT = new SimpleDateFormat("dd/MM/yy");
    date = DATE_FORMAT.format(today);
    System.out.println("Today in dd/MM/yy pattern : " + date);

    //formatting Date with time information
    DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS");
    date = DATE_FORMAT.format(today);
    System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);

    //SimpleDateFormat example - Date with timezone information
    DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS Z");
    date = DATE_FORMAT.format(today);
    System.out.println("Today in dd-MM-yy:HH:mm:SSZ : " + date);

} 

}

Output:

Today is : Fri Nov 02 16:11:27 IST 2012

Today in dd-MM-yyyy format : 02-11-2012

Today in dd/MM/yy pattern : 02/11/12

Today in dd-MM-yy:HH:mm:SS : 02-11-12:16:11:316

Today in dd-MM-yy:HH:mm:SSZ : 02-11-12:16:11:316 +0530

Nilesh Jadav
  • 890
  • 9
  • 8
2

IN two lines:

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm aa",Locale.getDefault());
        FAJR = new SimpleDateFormat("HH:mm",Locale.getDefault()).format(simpleDateFormat.parse("8:35 PM");
meduvigo
  • 1,523
  • 12
  • 7
1

just a tip,

try to use JodaTime instead of java,util.Date it is much more powerfull and it has a method toString("") that you can pass the format you want like toString("yyy-MM-dd HH:mm:ss");

http://joda-time.sourceforge.net/

fredcrs
  • 3,558
  • 7
  • 33
  • 55
1

With Calendar it works as follows:

    //create first Calendar object
    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.HOUR_OF_DAY, 15); // 3 PM
    // the same is
    calendar.set(Calendar.AM_PM, Calendar.PM); // choose PM mode
    calendar.set(Calendar.HOUR, 3); // 3 PM

    System.out.println( calendar.get(Calendar.HOUR) ); // AM_PM format
    System.out.println( calendar.get(Calendar.HOUR_OF_DAY) ); // 0-23 format
Andreas L.
  • 2,805
  • 23
  • 23
0

This is a method that uses JODA.

Input example is 07/20/2015 01:46:34.436 AM

public static String get24HourFormat(String dateString){
        Date date = new Date();
        DateTime date1=new DateTime();

        //  String date="";
        int hour=0;
        //int year=0;
        if(dateString!=null){

            try {
                DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm:ss.SSS aa");
                DateTimeFormatter output = DateTimeFormat.forPattern("kk");

                date1=formatter.parseDateTime(dateString);
                hour=date1.getHourOfDay();
                System.out.println(output.print(date1));
                //  System.out.println(date);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return hour+"";
    }
pnv
  • 1,437
  • 3
  • 23
  • 52