执行以下操作:
val zonedFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z")
ZonedDateTime.from(zonedFormatter.parse("31.07.2020 14:15 GMT"))
Run Code Online (Sandbox Code Playgroud)
给我:
"displayDate": "2020-07-31T14:15:00Z[GMT]"
Run Code Online (Sandbox Code Playgroud)
我想要没有 [GMT] 的:
"displayDate": "2020-07-31T14:15:00Z"
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 a 转换String为LocalDateusing DateTimeFormatter,但收到异常:
java.time.format.DateTimeParseException:无法在索引 5 处解析文本“ 2021-10-31 ”
我的代码是
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-uuuu");
String text = "2021-10-31";
LocalDate date = LocalDate.parse(text, formatter);
Run Code Online (Sandbox Code Playgroud)
我正在尝试从输入日期转换2021-10-31为31-Oct-2021.
java java-time localdate datetimeformatter datetimeparseexception
我想用这两个数字选择器更改月份和年份,但我不知道如何更改日期。我想要做的是:当我单击 BottomSheetDialog 上的“确定”按钮时,我想设置月份和年份。你能帮我吗?我尝试过,但在互联网上找不到任何解决方案。如果您帮助我,我将不胜感激。谢谢。
\npublic class PlannerFragment extends Fragment implements CalendarAdapter.OnItemListener{\n\nprivate TextView monthYearTextView;\nprivate TextView monthYearPickerOKTextView;\nprivate ImageView nextMonthImageView, previousMonthImageView;\nprivate RecyclerView calendarRecyclerView;\nprivate LocalDate selectedDate; /// tekrar buna bak\xc4\xb1lacak\nprivate NumberPicker monthNumberPicker, yearNumberPicker;\n\n@Override\npublic View onCreateView(LayoutInflater inflater, ViewGroup container,\n Bundle savedInstanceState) {\n getActivity().setTitle("Planner");\n View v = inflater.inflate(R.layout.fragment_planner, container, false);\n\n previousMonthImageView = v.findViewById(R.id.previous_month_image_view);\n nextMonthImageView = v.findViewById(R.id.next_month_image_view);\n\n calendarRecyclerView = v.findViewById(R.id.calendar_recycler_view);\n monthYearTextView = v.findViewById(R.id.month_year_text_view);\n\n selectedDate = LocalDate.now();\n\n setMonthYear();\n\n previousMonthImageView.setOnClickListener(new View.OnClickListener() {\n @Override\n public void onClick(View view) {\n selectedDate = selectedDate.minusMonths(1);\n setMonthYear();\n }\n });\n\n nextMonthImageView.setOnClickListener(new View.OnClickListener() {\n @Override\n …Run Code Online (Sandbox Code Playgroud) 以下正确解析文本输入“2022-12-29 01:16:03 GMT+08:00”。
public ZonedDateTime parseZonedDateTime(String timeStr) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss O");
ZonedDateTime zonedDateTime = ZonedDateTime.parse(timeStr, dtf);
return zonedDateTime;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果输入字符串是“2022-12-29 01:16:03 UTC-08:00”,ZonedDateTime.parse() 方法会抛出异常
java.time.format.DateTimeParseException: Text '2022-12-29 01:16:03 UTC-08:00' could not be parsed at index 20
Run Code Online (Sandbox Code Playgroud)
请访问 Ideone.com 查看在 Java 12 上运行的代码。
根据 DateTimeFormatter 文档,https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
“GMT+08:00”和“UTC-08:00”都被视为本地化区域偏移“O”。
正确的模式应该是什么,以便解析器接受 GMT 和 UTC 输入?
我尝试使用格式“yyyy-MM-dd HH:mm:ss OOOO”,但行为是相同的。
有一个类似的问题为什么“GMT+8”无法用模式“O”解析,尽管直接从文档中复制出来?据此,该错误已在 Java 9 build b116 中修复。
不过我正在使用 java 17.0.4.1-librca
出于测试目的,我想创建一个固定Clock时区:
Clock c= Clock.fixed(Instant.parse("2018-04-29T10:15:30.00Z"), ZoneId.of("Europe/Berlin"));
//yields FixedClock[2018-04-29T10:15:30Z,Europe/Berlin]
Run Code Online (Sandbox Code Playgroud)
现在我想使用Clock智慧LocalDateTime:
LocalDateTime.now(c);
//yields 2018-04-29T12:15:30
Run Code Online (Sandbox Code Playgroud)
为什么我要偏移两个小时?有没有 UTC 转换的地方?我需要做什么才能生成固定的Clock时区并LocalDate.now(c)产生相同的时间?
想象一下,我想要测试以下课程:
class Times {
Clock cl = Clock.systemDefaultZone();
public int changeTime() {
LocalDate ld = LocalDate.now(cl);
if(ld.getMonth().equals(Month.OCTOBER))
return 1;
if(ld.getMonth().equals(Month.NOVEMBER))
return 2;
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
如何强制日期在11月并断言该方法返回2?
我正在使用JUnit和Mockito.
使用Java 1.8.0_51以下代码(取自Unable从TemporalAccessor获取OffsetDateTime)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneId.of("Europe/Berlin"));
OffsetDateTime offsetDateTime = ZonedDateTime.parse("20151113", formatter).toOffsetDateTime();
System.out.println(offsetDateTime.format(DateTimeFormatter.ISO_DATE));
Run Code Online (Sandbox Code Playgroud)
抛出异常:
java.time.format.DateTimeParseException: Text '20151113' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO,Europe/Berlin resolved to 2015-11-13 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
Run Code Online (Sandbox Code Playgroud)
这次我做错了什么?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss z");
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
// 2016-10-10 09:28:45 PDT
String s = zonedDateTime.format(formatter);
// this call fails
ZonedDateTime.parse(s, formatter);
Run Code Online (Sandbox Code Playgroud)
给定片段有什么问题,不应该formatter.parse(date.format(formatter))评估相同date?
例外:
java.time.DateTimeException:无法从TemporalAccessor获取LocalTime:{MilliOfSecond = 0,MicroOfSecond = 0,HourOfAmPm = 9,MinuteOfHour = 28,NanoOfSecond = 0,SecondOfMinute = 45},ISO,America/Los_Angeles解析为2016-10- 10的java.time.format.Parsed类型
我是Mapstruct的新手。我有一个包含LocalDateTime类型字段的模型对象。DTO包含Instant类型字段。我想将LocalDateTime类型字段映射到Instant类型字段。我有传入请求的TimeZone实例。
像这样手动设置字段;
set( LocalDateTime.ofInstant(x.getStartDate(), timeZone.toZoneId()) )
Run Code Online (Sandbox Code Playgroud)
如何使用Mapstruct映射这些字段?
datetime-conversion java-time mapstruct localdate java.time.instant
我尝试以下代码:
import java.time.*;
...
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"dd-MMM-yyyy HH:mm:ss.n");
System.out.format("Now = %s %n", now.format(formatter));
Run Code Online (Sandbox Code Playgroud)
为了获得具有亚秒级信息的输出
Now = 12-Apr-2018 14:47:38.039578300
不幸的是,在每秒的前100毫秒中,亚秒信息的前导零被忽略了,并且我得到了非常误导的输出Now = 12-Apr-2018 14:47:38.39578300,很容易将其误解为大约38.4秒,即整秒后的396毫秒,而不是真实的。 38.04秒
我发现的唯一工作方法是ss.nnnnnnnnn正好为9 的格式n,以得到所需的输出。
编辑:
有一个更好的东西,我在发布此问题时错过了。
我对纳秒并不真正感兴趣,但是我真正想要的只是几分之一秒(大约毫秒分辨率)。
然后,这个更合适
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
Run Code Online (Sandbox Code Playgroud)
大写字母S表示亚秒数的位数,当然包括前导零。