java中从12小时制到24小时制的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6531632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conversion from 12 hours time to 24 hours time in java
提问by koti
In my app, I have a requirement to format 12 hours
time to 24 hours
time. What is the method I have to use?
在我的应用程序中,我需要12 hours
不时格式化24 hours
。我必须使用什么方法?
For example, time like 10:30 AM
. How can I convert to 24 hours time in java?
例如,时间像10:30 AM
。如何在java中转换为24小时时间?
采纳答案by Bart Kiers
Try this:
尝试这个:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String [] args) throws Exception {
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
Date date = parseFormat.parse("10:30 PM");
System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));
}
}
which produces:
它产生:
10:30 PM = 22:30
See: http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
请参阅:http: //download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
回答by fvu
Assuming that you use SimpleDateFormatimplicitly or explicitly, you need to use H
instead of h
in the format string.
假设您隐式或显式使用SimpleDateFormat,则需要在格式字符串中使用H
而不是h
。
E.g
例如
HH:mm:ss
HH:mm:ss
instead of
代替
hh:mm:ss
hh:mm:ss
回答by user3172824
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
provided by Bart Kiers answer should be replaced with somethig like
Bart Kiers 提供的答案应该替换为类似的东西
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a",Locale.UK);
回答by Usman Zafar
Try This
尝试这个
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;
}
回答by Cloud
java.time
时间
In Java 8 and later it could be done in one line using class java.time.LocalTime.
在 Java 8 及更高版本中,可以使用java.time.LocalTime类在一行中完成。
In the formatting pattern, lowercase hh
means 12-hour clock while uppercase HH
means 24-hour clock.
在格式化模式中,小写hh
表示 12 小时制,而大写HH
表示 24 小时制。
Code example:
代码示例:
String result = // Text representing the value of our date-time object.
LocalTime.parse( // Class representing a time-of-day value without a date and without a time zone.
"03:30 PM" , // Your `String` input text.
DateTimeFormatter.ofPattern( // Define a formatting pattern to match your input text.
"hh:mm a" ,
Locale.US // `Locale` determines the human language and cultural norms used in localization. Needed here to translate the `AM` & `PM` value.
) // Returns a `DateTimeFormatter` object.
) // Return a `LocalTime` object.
.format( DateTimeFormatter.ofPattern("HH:mm") ) // Generate text in a specific format. Returns a `String` object.
;
See this code run live at IdeOne.com.
15:30
15:30
See Oracle Tutorial.
请参阅Oracle 教程。
回答by RQube
I was looking for same thing but in number, means from integer xx hour, xx minutes and AM/PM to 24 hour format xx hour and xx minutes, so here what i have done:
我正在寻找相同的东西,但在数量上,意味着从整数 xx 小时、xx 分钟和 AM/PM 到 24 小时格式 xx 小时和 xx 分钟,所以这里我做了什么:
private static final int AM = 0;
private static final int PM = 1;
/**
* Based on concept: day start from 00:00AM and ends at 11:59PM,
* afternoon 12 is 12PM, 12:xxAM is basically 00:xxAM
* @param hour12Format
* @param amPm
* @return
*/
private int get24FormatHour(int hour12Format,int amPm){
if(hour12Format==12 && amPm==AM){
hour12Format=0;
}
if(amPm == PM && hour12Format!=12){
hour12Format+=12;
}
return hour12Format;
}`
private int minutesTillMidnight(int hour12Format,int minutes, int amPm){
int hour24Format=get24FormatHour(hour12Format,amPm);
System.out.println("24 Format :"+hour24Format+":"+minutes);
return (hour24Format*60)+minutes;
}
回答by venkata
Using LocalTime in Java 8, LocalTime has many useful methods like getHour()
or the getMinute()
method,
在 Java 8 中使用 LocalTime,LocalTime 有许多有用的方法,例如getHour()
或getMinute()
方法,
For example,
例如,
LocalTime intime = LocalTime.parse(inputString, DateTimeFormatter.ofPattern("h:m a"));
String outtime = intime.format(DateTimeFormatter.ISO_LOCAL_TIME);
In some cases, First line alone can do the required parsing
在某些情况下,仅第一行就可以完成所需的解析
回答by Ranjeet_Automation_Tester
Try this to calculate time difference between two times.
试试这个来计算两次之间的时差。
first it will convert 12 hours time into 24 hours then it will take diff between two times
首先它将 12 小时的时间转换为 24 小时然后它会在两次之间产生差异
String a = "09/06/18 01:55:33 AM";
String b = "07/06/18 05:45:33 PM";
String [] b2 = b.split(" ");
String [] a2 = a.split(" ");
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm:ss a");
String time1 = null ;
String time2 = null ;
if ( a.contains("PM") && b.contains("AM")) {
Date date = parseFormat.parse(a2[1]+" PM");
time1 = displayFormat.format(date);
time2 = b2[1];
}else if (b.contains("PM") && a.contains("AM")) {
Date date = parseFormat.parse(a2[1]+" PM");
time1 = a2[1];
time2 = displayFormat.format(date);
}else if (a.contains("PM") && b.contains("PM")){
Date datea = parseFormat.parse(a2[1]+" PM");
Date dateb = parseFormat.parse(b2[1]+" PM");
time1 = displayFormat.format(datea);
time2 = displayFormat.format(dateb);
}
System.out.println(time1);
System.out.println(time2);
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = format.parse(time1);
Date date2 = format.parse(time2);
long difference = date2.getTime() - date1.getTime();
System.out.println(difference);
System.out.println("Duration: "+DurationFormatUtils.formatDuration(difference, "HH:mm"));
For More Details Click Here
更多详情请点击这里
回答by Odatha Bandara
This is the extract of code that I have done.
这是我所做的代码摘录。
String s="08:10:45";
String[] s1=s.split(":");
int milipmHrs=0;
char[] arr=s1[2].toCharArray();
boolean isFound=s1[2].contains("PM");
if(isFound){
int pmHrs=Integer.parseInt(s1[0]);
milipmHrs=pmHrs+12;
return(milipmHrs+":"+s1[1]+":"+arr[0]+arr[1]);
}
else{
return(s1[0]+":"+s1[1]+":"+arr[0]+arr[1]);
}
回答by Manas Ranjan Mahapatra
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
public class Main {
public static void main(String [] args){
try {
DateFormat parseFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
String sDate = "22-01-2019 9:0:0 PM";
Date date = parseFormat.parse(sDate);
SimpleDateFormat displayFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
sDate = displayFormat.format(date);
LOGGER.info("The required format : " + sDate);
} catch (Exception e) {}
}
}