如何在日历中将时间设置为24小时格式

时间:2013-02-20 13:21:38

标签: java calendar

我需要在当前日期设置时间。时间字符串总是24小时格式,但我得到的结果是错误的:

  SimpleDateFormat df = new SimpleDateFormat("kk:mm");
  Date d1 = df.parse("10:30");
  Calendar c1 = Calendar.getInstance();
  c1.set(Calendar.HOUR, d1.getHours());
  c1.set(Calendar.MINUTE, d1.getMinutes());

日期应为今天的日期和时间设置为10:30。相反,c1中的时间最终为22:30。如何强制日历控件识别我的时间是24小时格式?

修改 如果我这样做:

Calendar c1 = Calendar.getInstance();
c1.set(Calendar.HOUR, 10);
c1.set(Calendar.MINUTE, 30);

这给了我相同的结果。为什么呢?

9 个答案:

答案 0 :(得分:172)

替换它:

c1.set(Calendar.HOUR, d1.getHours());

用这个:

c1.set(Calendar.HOUR_OF_DAY, d1.getHours());

Calendar.HOUR严格限制为12小时。

答案 1 :(得分:7)

使用SimpleDateFormat df = new SimpleDateFormat("HH:mm");代替

<强>更新

@Ingo是对的。最好使用setTime(d1);

第一种方法getHours()getMinutes()现已弃用

我测试了这段代码

SimpleDateFormat df = new SimpleDateFormat("hh:mm");
  Date d1 = df.parse("23:30");
  Calendar c1 = GregorianCalendar.getInstance();
  c1.setTime(d1);
  System.out.println(c1.getTime());

并且输出正常Thu Jan 01 23:30:00 FET 1970

试试这个

SimpleDateFormat df = new SimpleDateFormat("KK:mm aa");
  Date d1 = df.parse("10:30 PM");
  Calendar c1 = GregorianCalendar.getInstance(Locale.US);
  SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");  
  c1.setTime(d1);
  String str = sdf.format(c1.getTime());
  System.out.println(str);

答案 2 :(得分:3)

您可以使用

将日历设置为仅使用AM或PM
  

calendar.set(Calendar.AM_PM,int);

0 = AM

1 = PM

希望这有帮助

答案 3 :(得分:1)

我最近在我的项目中使用fullcalendar,我不知道你想要达到的确切视图效果,在我的项目中我想从12h格式更改事件时间视图

enter image description here

到24小时格式。

enter image description here

如果这是您想要达到的效果,下面的解决方案可能有所帮助:

设置timeFormat: 'H:mm'

答案 4 :(得分:1)

private void setClock(){

    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            CookieName = "social",
            CookieSecure = CookieSecureOption.Never
        });
    }

答案 5 :(得分:0)

试试这个:

SimpleDateFormat df = new SimpleDateFormat("hh:mm");

或者

SimpleDateFormat df = new SimpleDateFormat("KK:mm");

参考:SimpleDateFormat

答案 6 :(得分:0)

在这里,您将获得各种与时间相关的问题。我希望这能解决你的问题。

public class MyClass {

    public static void main(String[] args) {


        Calendar cal = Calendar.getInstance();

        // To get the current hour
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        System.out.println("hour: " + hour);

        // To get the current time in 12 hours format
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
        String a = sdf.format(cal.getTime());
        System.out.println("Time: " + a);

        // To get the desired time in 12 hours format from 23 hours format
        cal.set(Calendar.HOUR_OF_DAY, 24);
        SimpleDateFormat sdf1 = new SimpleDateFormat("hh:mm a",Locale.ENGLISH);
        String a1 = sdf1.format(cal.getTime());
        System.out.println("Time: " + a1);

        /*  H Hour in day (0-23) 
            k Hour in day (1-24) 
        */

        //To get the desired time in 24 hours format as 0-23 or 1-24
        cal.set(Calendar.HOUR_OF_DAY, 24);
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm",Locale.ENGLISH);
        SimpleDateFormat sdf3 = new SimpleDateFormat("kk:mm",Locale.ENGLISH);
        String a2 = sdf2.format(cal.getTime());
        String a3 = sdf3.format(cal.getTime());
        System.out.println("Time: " + a2);
        System.out.println("Time: " + a3);

        //For example, time like 12:30 PM. How can i convert to 24 hours time in java?

        SimpleDateFormat bigFormat = new SimpleDateFormat("kk:mm");
        SimpleDateFormat smallFormat = new SimpleDateFormat("hh:mm a");

        Date date = null;
        try {
            date = smallFormat.parse("12:30 AM");
        } catch (ParseException e) {
            e.printStackTrace();
        }        
        System.out.println(smallFormat.format(date) + " = " + bigFormat.format(date));

    }

}

答案 7 :(得分:0)

TL;博士

LocalTime.parse( "10:30" )  // Parsed as 24-hour time.

java.time

避免使用现在被java.time类取代的DateCalendar等麻烦的旧日期时间类。

LocalTime

java.time类提供了一种表示没有日期且没有时区的时间的方法:LocalTime

LocalTime lt = LocalTime.of( 10 , 30 );  // 10:30 AM.
LocalTime lt = LocalTime.of( 22 , 30 );  // 22:30 is 10:30 PM.

ISO 8601

生成和解析字符串时,java.time类默认使用标准的ISO 8601格式。这些格式使用24小时制。

String output = lt.toString();
  

LocalTime.of(10,30).toString():10:3​​0

     

LocalTime.of(22,30).toString():22:30

因此解析10:30将被解释为上午10:30。

LocalTime lt = LocalTime.parse( "10:30" );  // 10:30 AM.

DateTimeFormatter

如果您需要使用AM / PM以12小时单击格式生成或解析字符串,请使用DateTimeFormatter类。提示:养成指定Locale的习惯。

答案 8 :(得分:0)

如果替换为函数SimpleDateFormat(“ hh”) 与(“ HH”) 会将小时格式设置为24小时而不是12小时。

SimpleDateFormat df = new SimpleDateFormat("HH:mm");