jjzjj

DateTimeFormatter

全部标签

java - 添加到 DateTimeFormatterBuilder 的文字破折号导致解析失败

我正在尝试创建一个DateTimeFormatter以匹配以下示例(它实际上比这稍微复杂一些,但这无关紧要)。20180302-17:45:21我写了以下内容,但它导致了异常:newDateTimeFormatterBuilder().append(DateTimeFormatter.BASIC_ISO_DATE).appendLiteral('-').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter().parse("20180302-17:45:21");异常(exception)情况是:Exceptioninthread"m

java - 在java中格式化本地时间

我正在创建我的JavaFX应用程序,每次创建新的列表单元格时我都需要使用时间标签。我需要将当前时间的字符串以HH:MM格式直接放入以String作为参数的Label构造函数中。我发现并使用了java.util.Date的:LabeltimeLabel=newLabel(newSimpleDateFormat("HH:MM").format(newDate()));但是它显示了错误的时区,所以我打算使用java.time和LocalTime类。有没有办法在一行中实现相同的字符串结果?谢谢你的帮助:) 最佳答案 在新应用程序中使用Jav

java - 为什么 `GMT+8` 无法使用模式 `O` 进行解析,尽管是直接从文档中复制的?

为什么以下无法运行,日期时间字符串无法解析为OffsetDateTime?StringinputOdt="2016-01-23T12:34:56GMT+8";DateTimeFormatterformatterOdt=DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssO");OffsetDateTimeodt=OffsetDateTime.parse(inputOdt,formatterOdt);在MacOSXElCapitan10.11.4上使用Java(TM)SE运行时环境(build1.8.0_92-b14)。产生错误:Excep

java - 使用 DateTimeFormatter 显示短时区名称

我有以下DateTimeFormatter。DateTimeFormatterDATE_TIME_FORMATTER=DateTimeFormatter.ofPattern("MM/dd/yyyy'at'hh:mm:ssazzzz");我正在使用它来格式化ZonedDateTime,如下所示:ZonedDateTimedisableTime=Instant.now().plus(Duration.ofDays(21)).atZone(ZoneId.ofOffset("UTC",ZoneOffset.ofHours(-5)));System.out.println(DATE_TIME_F

java - 使用java8将字符串转换为日期

我正在尝试使用java8将字符串转换为日期格式。下面是我的代码。即使在提到格式模式为MM/dd/yyyy之后,我收到的输出也是yyyy/DD/MM格式。有人可以指出我做错了什么吗?Stringstr="01/01/2015";DateTimeFormatterformatter=DateTimeFormatter.ofPattern("MM/dd/yyyy");LocalDatedateTime=LocalDate.parse(str,formatter);System.out.println(dateTime); 最佳答案 那是因

java - 将 SimpleDateFormat 转换为 DateTimeFormatter

因此,当尝试使用SimpleDateFormat和Date替换一些遗留代码时,使用java.time.DateTimeFormatter和LocalDate我遇到了问题。这两种日期格式并不等同。在这一点上,我必须说我知道这两种日期类型不一样,但我所处的场景意味着我从不关心时间方面,所以可以忽略它。publicDategetDate(Stringvalue){SimpleDateFormatdateFormat=newSimpleDateFormat("dd/MM/yyyy");try{returndateFormat.parse(value);}catch(ParseException

Java LocalDateTime 解析错误

这个问题在这里已经有了答案:Howtoparsecase-insensitivestringswithJSR-310DateTimeFormatter?(3个答案)关闭6年前。已经尝试了4个小时来解决这个问题。:这行得通Stringdate="Jul-01-201409:10:12";LocalDateTimedt=LocalDateTime.parse(date,DateTimeFormatter.ofPattern("MMM-dd-yyyyHH:mm:ss",Locale.US));:这不会Stringdate="JUL-01-201409:10:12";LocalDateTime

java - DateTimeFormatter 与 SimpleDateFormat 不兼容

importjava.text.SimpleDateFormat;importjava.time.LocalDateTime;importjava.time.ZoneId;importjava.time.format.DateTimeFormatter;importjava.util.Date;publicclassTest001{publicstaticvoidmain(String[]args){Datedt=newDate();LocalDateTimelocalDateTime=LocalDateTime.now();DateTimeFormatterformatter=Dat

java - 如何将 UTC 偏移日期格式字符串解析为由 | 分隔的结果日期象征

我有一个非常特殊的问题,我正在尝试解析"2019-12-25T17:00:00-05:00"这样它应该给我结果DEC12|星期四|下午5:00我使用DateTimeFormatter和LocalDate尝试了以下代码DateTimeFormatterinputFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz",Locale.US);DateTimeFormatteroutputFormatter=DateTimeFormatter.ofPattern("MMd|E|hh:mma",Locale.US);Local

Java中DateTimeFormatter的使用方法和案例

🔔简介在Java中,DateTimeFormatter类用于格式化和解析日期时间对象。它是日期时间格式化的强大而灵活的工具。🔔作用🌵1.本地化时间本地化时间指根据指定的语言环境显示时间1.1.创建DateTimeFormatter时指定LocaleDateTimeFormatterformatter=DateTimeFormatter.ofPattern("yyyy-MM-dd",Locale.CHINA);1.2.使用该DateTimeFormatter格式化日期时间LocalDatedate=LocalDate.now();StringcnDate=date.format(formatte