微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

日历:如何获取字符串“10:00 AM”

如何解决日历:如何获取字符串“10:00 AM”

我正在尝试获取一个字符串,例如“上午 10:00”

int numberOfHourFrom_0_to_23=10;
Calendar m_auxCalendar = new GregorianCalendar();
m_auxCalendar.set(Calendar.HOUR,numberOfHourFrom_0_to_23 +12);//+12 makes it work correctly during PM hours,but not sure why.
m_auxCalendar.set(Calendar.MINUTE,0);

Date mdate=m_auxCalendar.getTime();
String mstring  = DateFormat.getTimeInstance(DateFormat.SHORT).format(mdate);

如果我在 PM 时间使用我的 Android 手机,此代码可以正常工作(我收到“10:00 AM”);但是,如果我在上午时间使用手机,我会收到“10:00 PM”而不是“10:00 AM”

解决方法

java.time 通过脱糖

考虑使用 java.time(现代 Java 日期和时间 API)来处理您的时间工作。

    DateTimeFormatter timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
    
    int numberOfHourFrom0To23 = 10;
    LocalTime time = LocalTime.of(numberOfHourFrom0To23,0);
    String mstring = time.format(timeFormatter);
    
    System.out.println(mstring);

美国语言环境的输出是(不出所料):

上午 10:00

LocalTime 是一天中没有日期的时间,所以这里似乎就是您所需要的。

您的代码出了什么问题?

意外地 Calendar.HOUR 指的是上午或下午的小时,从 0 到 11。我一直不明白这有什么用。当您在早上创建 GregorianCalendar 并将其 HOUR 设置为 10 时,您会得到上午 10 点。当你在下午做同样的事情时,你会得到晚上 10 点。您试图通过增加 12 小时来补偿。现在您将小时设置为 22,即使范围是 o 到 11。任何体面的类都会抛出 IllegalArgumentException(或类似的)。不是具有默认设置的 Calendar 对象。相反,它会将时间调整为第二天上午 10 点。现在,当您在早上运行代码时,您会看到晚上 10 点。

问题:java.time 不需要 Android API 级别 26 吗?

java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26 开始)中,内置了现代 API。
  • 在非 Android Java 6 和 7 中获得 ThreeTen Backport,现代类的向后移植(ThreeTen for JSR 310;请参阅底部的链接)。
  • 在较旧的 Android 上,使用脱糖或 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。在后一种情况下,请确保使用子包从 org.threeten.bp 导入日期和时间类。

链接

,

我以不同的方式解决了它。也许,它对你有帮助。但不幸的是,我无法在Android上尝试代码...

这是我的代码:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

// other code...

int numberOfHourFrom_0_to_23 = 15;
String hour = numberOfHourFrom_0_to_23 + ""; // you need a string for the following parsing
                
String mstring = ""; 
// you have to do it here,because in the other case you do it in the try block,// you wouldn't have access on it after finishing it
try{
    DateFormat hh = new SimpleDateFormat("hh"); 
    // your input date format,in this case only hours
    Date date = hh.parse(hour); 
    /* you have to parse the value of the string to a Date object,can throw a 'ParseException' exception --> try-catch */
            
    SimpleDateFormat targetFromat = new SimpleDateFormat("hh aa"); 
    // your target format,'aa' is for AM/PM
            
    mstring = targetFromat.format(date);
    // the method formats the time to the new format (incl. AM/PM)
} catch(ParseException e){
    // exception threw by parse in the class Date
    System.err.println("There was an error during parsing the initial string.");
    e.printStackTrace();
}
        
System.out.println(mstring); // test output,in this case: 03 PM

也许您对这篇文章感兴趣:https://beginnersbook.com/2017/10/java-display-time-in-12-hour-format-with-ampm/(这个答案对我有帮助)。

更新: 阅读@Ole V.V. 的评论后。 (谢谢你的指正!),我看了一下DateTimeFormatter这个类,我想在这里添加代码(比我的第一个代码简单多了!):

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

// other code...

int numberOfHourFrom_0_to_23 = 15; 

LocalTime time = LocalTime.of(numberOfHourFrom_0_to_23,0);
// parameters: hour,minute,second

String formatPattern = "hh a";
// the pattern,"hh" is for the two hours positions and "a" for AM/PM

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatPattern);
String mstring = formatter.format(time);
// formatting the value and solve it as a String

System.out.println(mstring); // test output,in this case: 03 PM

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。