【问题标题】:Convert 12 Hour to 24 Hour in JAVA within scope of UTC [closed]在UTC范围内将JAVA中的12小时转换为24小时[关闭]
【发布时间】:2015-01-29 09:58:54
【问题描述】:

请帮我将时间从 12 小时转换为 24 小时格式返回时间在 JAVA 中应该是 UTC

【问题讨论】:

    标签: java time utc


    【解决方案1】:

    这将帮助您将时间转换为 24 小时制

    public static String convertTo24Hour(String Time) {
        DateFormat f1 = new SimpleDateFormat("hh:mm a"); //11:00 pm
        Date d = null;
        try {
            d = f1.parse(Time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        DateFormat f2 = new SimpleDateFormat("HH:mm");
        String x = f2.format(d); // "23:00"
    
        return x;
    }
    

    稍后您可以根据自己的意愿转换为 UTC,或者您可以在一个函数中编译代码

    public static String CalculateUTC_Time(String Date, String Time) {
        String X[] = Time.split(":");
    
        int Hours = Integer.parseInt(X[0]);
        int Minutes = Integer.parseInt(X[1]);
    
        LocalDate date = new LocalDate(Date);
        LocalTime time = new LocalTime(Hours, Minutes);
        DateTime dt = date.toDateTime(time);
    
        SimpleDateFormat f2 = new SimpleDateFormat("HH:mm");
        f2.setTimeZone(TimeZone.getTimeZone("UTC"));
        return (f2.format(dt.toDate()));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2012-04-09
      • 2021-10-14
      相关资源
      最近更新 更多