将日期时间转换为24小时格式

时间:2011-07-27 09:40:50

标签: java datetime

我从服务器获得的时间就像Jul 27, 2011 8:35:29 AM

我想将其转换为yyyy-MM-dd HH:mm:ss

我还希望转换时间为24小时格式。任何人都可以解决这个问题。我想得到的输出就像2011-07-27 08:35:29

12 个答案:

答案 0 :(得分:150)

H vs h是24小时与12小时格式之间的差异。

答案 1 :(得分:110)

试试这个:

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);
}

答案 2 :(得分:85)

kk = Hours in 1-24 format
hh= hours in 1-12 format
KK= hours in 0-11 format
HH= hours in 0-23 format

答案 3 :(得分:10)

试试这个:

Date date=new Date("12/12/11 8:22:09 PM");    
System.out.println("Time in 24Hours ="+new SimpleDateFormat("HH:mm").format(date));

答案 4 :(得分:5)

您应该查看Java日期格式,特别是SimpleDateFormat。这里有一些例子:http://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html - 但你也可以通过快速谷歌找到更多。

答案 5 :(得分:4)

我以下面给出的日期为例,根据您的要求打印两种不同的日期格式。

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();
        }

如果您还有任何疑问,请回复。感谢。

答案 6 :(得分: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);

} 

}

输出:

今天是:2012年1月2日星期五16:11:27 IST 2012

今天以dd-MM-yyyy格式:02-11-2012

今天以dd / MM / yy模式:02/11/12

今天在dd-MM-yy:HH:mm:SS:02-11-12:16:11:316

今天在dd-MM-yy:HH:mm:SSZ:02-11-12:16:11:316 +0530

答案 7 :(得分:2)

分为两行:

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm aa",Locale.getDefault());
        FAJR = new SimpleDateFormat("HH:mm",Locale.getDefault()).format(simpleDateFormat.parse("8:35 PM");

答案 8 :(得分:1)

只是一个提示,

尝试使用JodaTime而不是java,util.Date 它更强大,它有一个方法 toString(“”),您可以传递您想要的格式 toString(“yyy-MM-dd HH:mm:ss”);

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

答案 9 :(得分:1)

使用Calendar,其工作方式如下:

    //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

答案 10 :(得分:1)

    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);

答案 11 :(得分:0)

这是一种使用JODA的方法。

输入示例为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+"";
    }
相关问题