-
java hh mm ss_java – 格式的解析时间hh:mm:ss
2021-03-04 02:33:18根据Basil Bourque的评论,考虑到Java 8的新API,这是这个问题的更新答案:String myDateString = "13:24:40";LocalTime localTime = LocalTime.parse(myDateString, DateTimeFormatter.ofPattern("HH:mm:ss"));int ...根据Basil Bourque的评论,考虑到Java 8的新API,这是这个问题的更新答案:
String myDateString = "13:24:40";
LocalTime localTime = LocalTime.parse(myDateString, DateTimeFormatter.ofPattern("HH:mm:ss"));
int hour = localTime.get(ChronoField.CLOCK_HOUR_OF_DAY);
int minute = localTime.get(ChronoField.MINUTE_OF_HOUR);
int second = localTime.get(ChronoField.SECOND_OF_MINUTE);
//prints "hour: 13, minute: 24, second: 40":
System.out.println(String.format("hour: %d, minute: %d, second: %d", hour, minute, second));
备注:
>由于OP的问题包含仅包含小时,分钟和秒(没有日,月等)的时间瞬间的具体示例,上面的答案仅使用LocalTime.如果想要解析也包含天,月的字符串,等等,然后需要LocalDateTime.它的用法非常类似于LocalTime.
>由于时间瞬间int OP的问题不包含任何有关时区的信息,答案使用LocalXXX版本的日期/时间类(LocalTime,LocalDateTime).如果需要解析的时间字符串也包含时区信息,则需要使用ZonedDateTime.
======以下是此问题的旧(原始)答案,使用pre-Java8 API:=====
我很抱歉,如果我对这个人感到不安,但我真的要回答这个问题. Java API非常庞大,我认为有人可能会偶尔错过一个.
SimpleDateFormat可以在这里做到这一点:
它应该是这样的:
String myDateString = "13:24:40";
//SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
//the above commented line was changed to the one below, as per Grodriguez's pertinent comment:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = sdf.parse(myDateString);
Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
calendar.setTime(date); // assigns calendar to given date
int hour = calendar.get(Calendar.HOUR);
int minute; /... similar methods for minutes and seconds
你应该知道的陷阱:
>传递给SimpleDateFormat的模式可能与我的示例中的模式不同,具体取决于您拥有的值(12小时格式或24小时格式的小时数等).请查看链接中的文档以获取详细信息
>一旦你从你的String创建一个Date对象(通过SimpleDateFormat),不要试图使用Date.getHour(),Date.getMinute()等.它们似乎有时会工作,但总的来说它们可以给出错误结果,因此现在已弃用.请使用日历,如上例所示.
-
java mm框架_java – 格式的解析时间hh:mm:ss
2021-03-18 10:47:47根据Basil Bourque的评论,考虑到Java 8的新API,这是这个问题的更新答案:String myDateString = "13:24:40";LocalTime localTime = LocalTime.parse(myDateString, DateTimeFormatter.ofPattern("HH:mm:ss"));int ...根据Basil Bourque的评论,考虑到Java 8的新API,这是这个问题的更新答案:
String myDateString = "13:24:40";
LocalTime localTime = LocalTime.parse(myDateString, DateTimeFormatter.ofPattern("HH:mm:ss"));
int hour = localTime.get(ChronoField.CLOCK_HOUR_OF_DAY);
int minute = localTime.get(ChronoField.MINUTE_OF_HOUR);
int second = localTime.get(ChronoField.SECOND_OF_MINUTE);
//prints "hour: 13, minute: 24, second: 40":
System.out.println(String.format("hour: %d, minute: %d, second: %d", hour, minute, second));
备注:
>由于OP的问题包含仅包含小时,分钟和秒(没有日,月等)的时间瞬间的具体示例,上面的答案仅使用LocalTime.如果想要解析也包含天,月的字符串,等等,然后需要LocalDateTime.它的用法非常类似于LocalTime.
>由于时间瞬间int OP的问题不包含任何有关时区的信息,答案使用LocalXXX版本的日期/时间类(LocalTime,LocalDateTime).如果需要解析的时间字符串也包含时区信息,则需要使用ZonedDateTime.
======以下是此问题的旧(原始)答案,使用pre-Java8 API:=====
我很抱歉,如果我对这个人感到不安,但我真的要回答这个问题. Java API非常庞大,我认为有人可能会偶尔错过一个.
SimpleDateFormat可以在这里做到这一点:
它应该是这样的:
String myDateString = "13:24:40";
//SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
//the above commented line was changed to the one below, as per Grodriguez's pertinent comment:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = sdf.parse(myDateString);
Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
calendar.setTime(date); // assigns calendar to given date
int hour = calendar.get(Calendar.HOUR);
int minute; /... similar methods for minutes and seconds
你应该知道的陷阱:
>传递给SimpleDateFormat的模式可能与我的示例中的模式不同,具体取决于您拥有的值(12小时格式或24小时格式的小时数等).请查看链接中的文档以获取详细信息
>一旦你从你的String创建一个Date对象(通过SimpleDateFormat),不要试图使用Date.getHour(),Date.getMinute()等.它们似乎有时会工作,但总的来说它们可以给出错误结果,因此现在已弃用.请使用日历,如上例所示.
-
java 时间解析
2015-12-31 16:53:27对于以下格式的日期进行转换成正常的2014-12-11 11:20:44: Thu, 31 Dec 2015 13:05:30 +0800 Thu, 31 Dec 2015 08:25:04 GMT 使用如下代码进行转换: ... "EEE, d MMM yyyy HH:mm:ss 'GMT'",对于以下格式的日期进行转换成正常的2014-12-11 11:20:44:
Thu, 31 Dec 2015 13:05:30 +0800 Thu, 31 Dec 2015 08:25:04 GMT
使用如下代码进行转换:SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US); SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss z", Locale.US); Date parse = null; try { parse = sdf.parse(feed.getItem(i).getPubdate()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String messagetimePar = sdf.format(parse);
-
java localdatetime解析_Java LocalDateTime解析
2021-03-08 17:20:58为什么我可以用无效小时解析java中的日期时间字符串?我错过或需要做些什么来确保它适当地抛出错误.以下代码不会抛出错误,它应该在哪里?DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("uuuu-...为什么我可以用无效小时解析java中的日期时间字符串?我错过或需要做些什么来确保它适当地抛出错误.
以下代码不会抛出错误,它应该在哪里?
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss");
LocalDateTime aFormattedDate = LocalDateTime.parse("2019-01-01T24:00:00", dateTimeFormatter); // returns 2019-01-02T00:00:00, should throw an error
将小时指定为25,或包括任何毫秒或其他时间组件会导致解析引发错误.
在哪里
LocalDateTime aDate = LocalDateTime.parse("2019-01-01T24:00:00"); //throws an error
抛出错误 – 关于HourOfDay需要在0到23之间 – 正如预期的那样
解决方法:
ResolverStyle
因为如果未指定解析器样式,DateTimeFormatter.ofPattern()默认为ResolverStyle.SMART. SMART允许一些转换. 24:00:00将转换为第二天,但24:00:01将抛出异常.根据枚举javadoc:
Style to resolve dates and times in a smart, or intelligent, manner.
Using smart resolution will perform the sensible default for each field, which may be the same as strict, the same as lenient, or a third behavior. Individual fields will interpret this differently.
For example, resolving year-month and day-of-month in the ISO calendar system using smart mode will ensure that the day-of-month is from 1 to 31, converting any value beyond the last valid day-of-month to be the last valid day-of-month.
LocalDateTime.parse()在引擎盖下使用ResolveStyle.STRICT,使其等效于:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss")
.withResolverStyle(ResolverStyle.STRICT);
LocalDateTime.parse("2019-01-01T24:00:00", fmt); // DateTimeParseException
标签:java,java-time,validation,datetime-parsing
来源: https://codeday.me/bug/20190627/1302804.html
-
java 日期 解析_全面解析Java日期时间API
2021-02-12 22:34:29时区GMT(Greenwich Mean...UTC(Universal Time Coordinated):统一协调时间,其以原子时秒长为基础,在时刻上尽量接近于格林尼治标准时间,标准 UTC 时间格式 yyyy-MM-dd'T'HH:mm:ss.SSSXXX。格林尼治时间已经不再被... -
java时间解析错误_ParseException:无法解析的日期异常
2021-03-08 00:02:26博士LocalDateTime.parse("Feb 13, 2017 10:25:43 AM" ,DateTimeFormatter.ofPattern( "MMM d, uuuu hh:mm:ss a" , Locale.US ))2017-02-13T10:25:43java.time您正在使用现在遗留的麻烦的旧日期时间类,取而代之的... -
java多重解析日期_全面解析Java日期时间API
2021-03-14 14:55:27时区GMT(Greenwich Mean...UTC(Universal Time Coordinated):统一协调时间,其以原子时秒长为基础,在时刻上尽量接近于格林尼治标准时间,标准 UTC 时间格式 yyyy-MM-dd'T'HH:mm:ss.SSSXXX。格林尼治时间已经不再被... -
java日期时间解析
2015-11-11 11:36:26例如我要解析的String类型的时间Wed, 11 Nov 2015 00:02:00 GMT,解析如下String content = “Wed, 11 Nov 2015 00:02:00 GMT”; SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ... -
java 解析时间字符串_Java中的解析日期字符串
2021-03-09 23:04:32String date="2018-07-17T09:59:...SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sssZ");Date transactionDateTime = simpleDateFormat.parse(date);这给了我“Unparseable d... -
Java 解析UTC格式时间为时间戳
2019-09-12 11:00:58Java 解析UTC格式时间为时间戳 String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC... -
java8 日期时间解析与转换
2018-12-26 11:35:00Instant now = Instant.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()); String formated = formatter.format... -
java解析形如yyyy-MM-dd'T'HH:mm:ss.SSS'Z'的时间格式
2017-03-13 14:00:06String STANDARD_DATE_FORMAT_...yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; Calendar date = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DATE_FORMAT_UTC); sdf.setTimeZone(TimeZo... -
全面解析Java日期时间API
2019-06-01 19:53:00时区 GMT(Greenwich Mean ...UTC(Universal Time Coordinated):统一协调时间,其以原子时秒长为基础,在时刻上尽量接近于格林尼治标准时间,标准 UTC 时间格式 yyyy-MM-dd'T'HH:mm:ss.SSSXXX。 格林尼治时间... -
java dateformat gmt_Java解析GMT格式的时间 | 学步园
2021-02-26 20:34:25private String pattern = "EEE, dd MMM yyyy HH:mm:ss z";private SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);// String dateStr = "Tue, 26 Feb 2013 09:26:57 GMT";Date date = format... -
java获取时间 t00:00:00.0z_全面解析Java日期时间API
2021-03-18 00:52:45时区GMT(Greenwich Mean...UTC(Universal Time Coordinated):统一协调时间,其以原子时秒长为基础,在时刻上尽量接近于格林尼治标准时间,标准 UTC 时间格式 yyyy-MM-dd'T'HH:mm:ss.SSSXXX。格林尼治时间已经不再被... -
java解析和格式化_Java中日期的解析与格式化
2021-03-11 14:19:59本文以CST时间的解析和格式化为例:将CST时间的字符串格式化成某个指定模板的时间格式。该CST时间字符串为"Tue Jul 31 13:57:33 CST 2012",可以 使用 java.text.SimpleDateFormat类来完成。1、使用"yyyy-MM-dd HH:... -
Java解析GMT格式的时间
2013-02-28 15:38:52private String pattern = "EEE, dd MMM yyyy HH:mm:ss z"; private SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US); // String dateStr = "Tue, 26 Feb 2013 09:26:57 GMT"; Date date = for -
java 函数 微秒_在Java中以微秒为单位解析时间
2021-03-09 17:14:26我在解析2013-01-09 09:15:03.000000格式的Java时间字符串时遇到问题。 在我的数据中,最后三个数字始终为0(表示输入字符串仅具有毫秒精度),因此我将此格式传递给SimpleDateFormat:formatter = new ... -
Java解析CST时间出现ParseException的解决方法?
2020-01-08 19:29:43今天线上出了个bug,发现是在解析CST格式的时间时,出了一个bug。 后来发现 一是需要指定对应的格式 "EE MMM dd HH:mm:ss z yyyy" 二是需要指定时间格式 Locale.ENGLISH 注:我们在做时间格式转换时,主要是找对... -
java date的精度_关于simpledateformat:具有微秒或纳秒精度的Java日期解析
2021-03-08 06:52:07根据SimpleDateFormat类文档,Java在其日期模式中不支持毫秒以上的时间粒度。因此,日期字符串2015-05-09 00:10:23.999750900 //最后9位数字表示纳秒通过模式解析时yyyy-MM-dd HH:mm:ss.SSSSSSSSS // 9个'S'符号... -
java 时间周期控制器_无法解析Spring控制器中的本地日期时间
2021-03-16 11:58:08在我的Spring Boot应用程序中,我需要处理一个带有日期时间字段的表单,并将其转换为Java中的 LocalDateTime .我指定了模式 "YYYY-MM-dd HH:mm" ,当我提交带有输入值 1990-01-01 10:10 的表单时,它无法转换 .这是... -
java 时间字符串中毫秒值时有时无,怎么解析
2021-02-26 10:15:24java 时间字符串中毫秒值时有时无,怎么解析 针对某个值 时有时无的情况,可用 [xxx] 括起来,标识 public void testDate2() { // 针对某个值 时有时无的情况,可用 [] 括起来,标识 DateTimeFormatter ... -
Java 解析中间带有 “T“ 的时间格式
2020-08-03 18:26:24public String convertDataForT... SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"); // Data 类型 // Date parse = simpleDateFormat.parse(inputData); // String 类型 ... -
全面解析Java日期时间API (转抄)
2019-09-16 15:49:44时区 GMT(Greenwich Mean ...UTC(Universal Time Coordinated):统一协调时间,其以原子时秒长为基础,在时刻上尽量接近于格林尼治标准时间,标准 UTC 时间格式 yyyy-MM-dd'T'HH:mm:ss.SSSXXX。 格林尼治时间已... -
java kotlin解析2018-12-28T09:17:30.875+0000这种类型的时间
2019-01-16 10:46:40fun praseTime(time: String): String { val df = SimpleDateFormat...yyyy-MM-dd'T'hh:mm:ss.SSSZ", Locale.CHINA) val result: Date result = df.parse(time) val sdf = SimpleDateFormat("yyyy-MM... -
Java解析与格式化注解
2019-04-19 10:59:00Spring时间解析注解——来源Spring框架自带 @DateTimeFormat org.springframework.format.annotation.DateTimeFormat 功能:字符串格式化成日期,常用于前台向后端传参,解析json串 支持常见标准时间格式:①yyyy... -
Java 时间
2019-05-08 20:02:07Date本身提供的数据已经属于一个完整的日期时间,需要指定格式化的模板: 年 (yyyy )、月 (MM )、日(dd )、时 (HH )、分 (mm )、秒 (ss )、毫秒 (SSS) ...parse() 方法可解析一个日期时间字符串... -
java 解析sh文件内容_一个shell解析文件的需求
2021-02-28 17:02:56点击标题下「蓝色微信名」可快速关注预计阅读时间:7分钟由于Oracle没有MySQL或EDB慢日志这种机制,因此最近有一个运维的需求,需要解析应用的日志,进行SQL语句执行时间的统计,进而进行监控。每个应用集群中的节点...