简体   繁体   English

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

[英]How to set time to 24 hour format in Calendar

I need to set the time on the current date.我需要在当前日期设置时间。 The time string is always in 24 hour format but the result I get is wrong:时间字符串始终采用 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());

The date should be today's date and the time set to 10:30.日期应该是今天的日期,时间设置为 10:30。 Instead the time in c1 ends up being 22:30.相反,c1 中的时间最终是 22:30。 How can I force the calendar control to recognize my time is 24 hour format?如何强制日历控件识别我的时间是 24 小时格式?

EDIT: If I just do this:编辑:如果我这样做:

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

This gives me the same result.这给了我同样的结果。 Why?为什么?

Replace this:替换这个:

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

with this:有了这个:

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

Calendar.HOUR is strictly for 12 hours. Calendar.HOUR 严格为 12 小时。

use SimpleDateFormat df = new SimpleDateFormat("HH:mm");使用SimpleDateFormat df = new SimpleDateFormat("HH:mm"); instead反而

UPDATE更新

@Ingo is right. @Ingo 是对的。 is's better use setTime(d1);最好使用setTime(d1);

first method getHours() and getMinutes() is now deprecated第一个方法getHours()getMinutes()现在已弃用

I test this code我测试这段代码

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

and output is ok Thu Jan 01 23:30:00 FET 1970并且输出正常Thu Jan 01 23:30:00 FET 1970

try this试试这个

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

You can set the calendar to use only AM or PM using您可以使用以下命令将日历设置为仅使用 AM 或 PM

calendar.set(Calendar.AM_PM, int);日历。设置(日历。AM_PM,int);

0 = AM 0 = 上午

1 = PM 1 = 下午

Hope this helps希望这可以帮助

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

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

I am using fullcalendar on my project recently, I don't know what exact view effect you want to achieve, in my project I want to change the event time view from 12h format from我最近在我的项目中使用 fullcalendar,我不知道你想要达到什么确切的视图效果,在我的项目中我想从 12h 格式更改事件时间视图

在此处输入图片说明

to 24h format.到 24 小时格式。

在此处输入图片说明

If this is the effect you want to achieve, the solution below might help:如果这是您想要达到的效果,下面的解决方案可能会有所帮助:

set timeFormat: 'H:mm'设置时间timeFormat: 'H:mm'

Here you will get all kinds of time related problems.在这里,您会遇到各种与时间相关的问题。 I hope this will solve your problem.我希望这能解决你的问题。

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

    }

}

private void setClock() {私有无效 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();
}

Try this :试试这个 :

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

Or要么

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

Reference : SimpleDateFormat参考: SimpleDateFormat

tl;dr tl;博士

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

java.time时间

Avoid the troublesome old date-time classes such as Date and Calendar that are now supplanted by the java.time classes.避免麻烦的旧日期时间类,例如现在被 java.time 类取代的DateCalendar

LocalTime

The java.time classes provide a way to represent the time-of-day without a date and without a time zone: 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 ISO 8601

The java.time classes use standard ISO 8601 formats by default when generating and parsing strings. java.time 类在生成和解析字符串时默认使用标准 ISO 8601 格式。 These formats use 24-hour time.这些格式使用 24 小时制。

String output = lt.toString();

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

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

So parsing 10:30 will be interpreted as 10:30 AM.因此解析10:30将被解释为 10:30 AM。

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

DateTimeFormatter

If you need to generate or parse strings in 12-hour click format with AM/PM, use the DateTimeFormatter class.如果您需要使用 AM/PM 以 12 小时点击格式生成或解析字符串,请使用DateTimeFormatter类。 Tip: make a habit of specifying a Locale .提示:养成指定Locale的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM