无法更改 AM/PM 12:00 之后的时间(AfterNoon)
Cannot change AM/PM of Time After 12:00 (AfterNoon)
如何将 24 小时格式与 AM/PM 一起转换为 12 小时制,我已经检查了文章,也在 Whosebug 中,但问题是如果字符串是“12:43”(下午时间)并且我将其转换进入 12 小时然后显示“12:43 AM”但它应该显示“12:43 PM”。下面是我写的 java 代码。
MainActivity.Java
public class PrayerActivity extends AppCompatActivity {
TextView result;
String fetch="12:43"
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result=findViewById(R.id.result);
Date dateCode = null;
String formatTwelve;
String resultsAfter;
final SimpleDateFormat code12Hours = new SimpleDateFormat("hh:mm");
try {
dateCode = code12Hours.parse(fetch);
} catch (ParseException e) {
e.printStackTrace();
}
formatTwelve = code12Hours.format(dateCode);
if (formatTwelve.equals(fetch)) {
resultsAfter= formatTwelve + " AM";
} else {
resultsAfter= formatTwelve + " PM";
}
result.setText(resultsAfter);
}}
您所要做的就是使用 "h:mm a"
或 "hh:mm aa"
以获得更多详细信息或代码,您可以按照此 gist link 来更好地理解。
try {
String timeLong = "2020-10-08 06:06:30";
String timeShort = "16:06 AM";
SimpleDateFormat formatLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
SimpleDateFormat formatShort = new SimpleDateFormat("hh:mm aa", Locale.US);
Log.v("out", formatShort.format(formatLong.parse(timeLong)));
Log.v("out", formatShort.format(formatShort.parse(timeShort)));
} catch (ParseException e) {
e.printStackTrace();
}
输出:
06:06上午
04:06下午
public static void main(String[] args) {
String[] stringDates = { "12:43", "14:00", "10:00" };
for (String dateString : stringDates) {
// Input 24 HRS
LocalTime localTime = LocalTime.parse(dateString, DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH));
// Output 12 hrs
System.out.println(localTime.format(DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH)));
}
}
输出
12:43 PM
02:00 PM
10:00 AM
如何将 24 小时格式与 AM/PM 一起转换为 12 小时制,我已经检查了文章,也在 Whosebug 中,但问题是如果字符串是“12:43”(下午时间)并且我将其转换进入 12 小时然后显示“12:43 AM”但它应该显示“12:43 PM”。下面是我写的 java 代码。
MainActivity.Java
public class PrayerActivity extends AppCompatActivity {
TextView result;
String fetch="12:43"
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result=findViewById(R.id.result);
Date dateCode = null;
String formatTwelve;
String resultsAfter;
final SimpleDateFormat code12Hours = new SimpleDateFormat("hh:mm");
try {
dateCode = code12Hours.parse(fetch);
} catch (ParseException e) {
e.printStackTrace();
}
formatTwelve = code12Hours.format(dateCode);
if (formatTwelve.equals(fetch)) {
resultsAfter= formatTwelve + " AM";
} else {
resultsAfter= formatTwelve + " PM";
}
result.setText(resultsAfter);
}}
您所要做的就是使用 "h:mm a"
或 "hh:mm aa"
以获得更多详细信息或代码,您可以按照此 gist link 来更好地理解。
try {
String timeLong = "2020-10-08 06:06:30";
String timeShort = "16:06 AM";
SimpleDateFormat formatLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
SimpleDateFormat formatShort = new SimpleDateFormat("hh:mm aa", Locale.US);
Log.v("out", formatShort.format(formatLong.parse(timeLong)));
Log.v("out", formatShort.format(formatShort.parse(timeShort)));
} catch (ParseException e) {
e.printStackTrace();
}
输出: 06:06上午 04:06下午
public static void main(String[] args) {
String[] stringDates = { "12:43", "14:00", "10:00" };
for (String dateString : stringDates) {
// Input 24 HRS
LocalTime localTime = LocalTime.parse(dateString, DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH));
// Output 12 hrs
System.out.println(localTime.format(DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH)));
}
}
输出
12:43 PM
02:00 PM
10:00 AM