【问题标题】:How to set time to 24 hour format in Calendar如何在日历中将时间设置为 24 小时格式
【发布时间】:2013-02-20 13:21:38
【问题描述】:

我需要将时间设置在当前日期。时间字符串始终为 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);

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

【问题讨论】:

  • setTime 需要一个日期。日期最终会覆盖日历的日期。
  • 我的时间总是 24 小时制。该帖子清楚地表明了上午/下午格式。
  • getHours()getMinutes() 方法已弃用。看起来你不需要使用它们。如果你只是将 10 和 30 传递给Calendar setter 会发生什么?
  • 我刚试过,结果是一样的。显然 Calendar 对象发生了一些愚蠢的事情。

标签: java calendar


【解决方案1】:

替换这个:

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

用这个:

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

Calendar.HOUR 严格为 12 小时。

【讨论】:

  • +1 Javadoc 说得很清楚“用于获取和设置的字段编号指示一天中的小时。HOUR_OF_DAY 用于 24 小时制。例如,在晚上 10:04:15.250 HOUR_OF_DAY是 22 岁。”
【解决方案2】:

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

【讨论】:

  • 同样的结果。我仍然以 22:30 结束。
  • 您使用的是 23:30。试试 10:30。如果你的替代 10:30 给你 10:30,那么它告诉我这可能是一个平台问题。
  • 可能我不明白你想要什么,但看看我的最后一个例子
  • 正如我所发布的,我的时间总是 24 小时。 10:30 是早上的 10:30,而不是像您所做的那样在晚上。我不使用上午/下午。
  • 所以如果我在 sysout 中设置 10.30 我有 10.30 如果我在 sysiut 中设置 22.30 我有 22.30
【解决方案3】:

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

calendar.set(Calendar.AM_PM, int);

0 = 上午

1 = 下午

希望对你有帮助

【讨论】:

    【解决方案4】:

    如果您在函数 SimpleDateFormat("hh") 中替换 与(“HH”) 将把小时格式化为 24 小时而不是 12 小时。

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

    【讨论】:

      【解决方案5】:

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

      转为 24 小时制。

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

      设置timeFormat: 'H:mm'

      【讨论】:

        【解决方案6】:

        在这里你会遇到各种时间相关的问题。我希望这能解决您的问题。

        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】:

          私有 void setClock() {

              Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
                  Calendar cal = Calendar.getInstance();
                  int second = cal.get(Calendar.SECOND);
                  int minute = cal.get(Calendar.MINUTE);
                  int  hour = cal.get(Calendar.HOUR_OF_DAY);
                  eski_minut = minute;
                  if(second < 10){
          
                      time_label.setText(hour + ":" + (minute) + ":0" + second);
          
                  }else if (minute < 10){
                      time_label.setText(hour + ":0" + (minute) + ":0" + second);
          
                  }
                  else {
                  time_label.setText(hour + ":" + (minute) + ":" + second);}
              }),
                      new KeyFrame(Duration.seconds(1))
              );
              clock.setCycleCount(Animation.INDEFINITE);
              clock.play();
          }
          

          【讨论】:

          • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
          【解决方案8】:

          试试这个:

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

          或者

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

          参考:SimpleDateFormat

          【讨论】:

          • 这些都不起作用。结果相同:22:30。他们需要将 SimpleDateFormat 重命名为 ComplexAndStupidDateFormat。
          【解决方案9】:

          tl;博士

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

          java.time

          避免使用麻烦的旧日期时间类,例如 DateCalendar,它们现在已被 java.time 类所取代。

          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:30

          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 的习惯。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-05-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-02-01
            • 1970-01-01
            相关资源
            最近更新 更多