我想解析这样的日期字符串2011-11-30:
LocalDateTime.parse("2011-11-30", DateTimeFormatter.ISO_LOCAL_DATE)
Run Code Online (Sandbox Code Playgroud)
但我得到以下异常:
java.time.format.DateTimeParseException: Text '2011-11-30' could not be parsed:
Unable to obtain LocalDateTime from TemporalAccessor
Run Code Online (Sandbox Code Playgroud)
如果我传递一个日期和时间字符串,一切都按预期工作:
LocalDateTime.parse("2011-11-30T23:59:59", DateTimeFormatter.ISO_LOCAL_DATE_TIME)
Run Code Online (Sandbox Code Playgroud)
如何将日期解析为2011-11-30LocalDateTime(具有默认时间)?
我DateUtils.getRelativeTimeSpanString()用于在发布帖子时显示。
它返回的字符串是这样的:1 minute ago,但我想要1 m或1 h等等(缩短并且没有之前)。
我可以用“h”替换“hour”或用“m”替换“minute”,但如果语言与英语不同,它将不起作用。这就是现在Twitter正在使用的。Twitter 的英文/西里尔文显示方式。
更新:我会接受@Bradley Wilson 的回答,尽管我会在这里添加更清洁的解决方案(再次使用 JodaTime 包)。此外,其余的答案也可以修改为相同的结果,因此他们值得投票。谢谢你们:
DateTime postMaded = new DateTime(your previous date);
DateTime nowUpdate = new DateTime();
Period period = new Period(postMaded, nowUpdate);
PeriodFormatter formatter;
Locale current = ConverterMethods.getCurrentLocale();
if (current.getLanguage().contentEquals(any Cyrillic language )) {
if (period.getYears() != 0) {
formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" ?.").printZeroNever().toFormatter();
} else if (period.getMonths() != 0) {
formatter = new PeriodFormatterBuilder().appendMonths().appendSuffix(" ?").printZeroNever().toFormatter();
} else …Run Code Online (Sandbox Code Playgroud) 我有这个方法来计算midnigt和当前时间作为长值:
/**
* Returns the time range between the midnight and current time in milliseconds.
*
* @param zoneId time zone ID.
* @return a {@code long} array, where at index: 0 - midnight time; 1 - current time.
*/
public static long[] todayDateRange(ZoneId zoneId) {
long[] toReturn = new long[2];
LocalTime midnight = LocalTime.MIDNIGHT;
LocalDate today = LocalDate.now(zoneId);
LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
ZonedDateTime todayMidnightZdt = todayMidnight.atZone(zoneId);
toReturn[0] = todayMidnightZdt.toInstant().toEpochMilli();
ZonedDateTime nowZdt = LocalDateTime.now().atZone(zoneId);
toReturn[1] = nowZdt.toInstant().toEpochMilli();
return toReturn; …Run Code Online (Sandbox Code Playgroud) 我输入了许多不同格式的日期。我基本上是在尝试采用所有不同的格式并将它们标准化为 ISO 8601 格式。
如果日期包含月份名称,例如三月,那么我将使用以下函数来获取月份编号,例如 03。
month = String.valueOf(Month.valueOf(month.toUpperCase()).getValue());
Run Code Online (Sandbox Code Playgroud)
无论如何,我遇到的问题是月份名称有许多不同的语言,没有说明它们将是什么语言。运行上述函数时出现以下错误:
Caused by: java.lang.IllegalArgumentException: No enum constant java.time.Month.AUGUSTI
at java.lang.Enum.valueOf(Enum.java:238)
at java.time.Month.valueOf(Month.java:106)
Run Code Online (Sandbox Code Playgroud)
是否有任何库可以处理多种不同语言的月份名称,返回数值,甚至只是将月份名称翻译成英文?
这是输入日期的示例:
1370037600
1385852400
1356994800
2014-03-01T00:00:00
2013-06-01T00:00:00
2012-01-01
2012
May 2012
März 2010
Julio 2009
Run Code Online (Sandbox Code Playgroud) 我有一个查询参数如下:
@GET
public Response myFunction(@QueryParam("start") final LocalDate start, @QueryParam("end") final LocalDate end) { ... }
Run Code Online (Sandbox Code Playgroud)
为此,我创建了一个ParamConverter<LocalDate>将字符串转换为日期,反之亦然。
现在我想使用@DefaultValue注解来声明一个默认值。我有两个特殊的默认值:
start)end)是否可以为此使用@DefaultValue注释?如何?
我想ZonedDateTime在解析数组列表的元素后将它们转换为对象。下面显示了一个字符串。
"2017-02-12 06:59:00 +1300"
Run Code Online (Sandbox Code Playgroud)
目前我使用DateTimeFormatter:
DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss ZZ");
Run Code Online (Sandbox Code Playgroud)
并尝试使用parse, 来获取时间:
this.actionTime = dateTimeFormatter.parse(actionTime, ZonedDateTime::from);
Run Code Online (Sandbox Code Playgroud)
看下面的方法:
public DateCalculatorTest(String actionTime, int expectedDayOfWeek) {
DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss ZZ");
DateTimeFormatter localTimeFormatter =
DateTimeFormatter.ofPattern("YYYY-MM-dd");
this.actionTime = dateTimeFormatter.parse(actionTime, ZonedDateTime::from);
this.expectedDayOfWeek = expectedDayOfWeek;
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法解析字符串。我收到以下错误:
Text '2017-02-12 06:59:00 +1300' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2017, DayOfMonth=12, MonthOfYear=2, OffsetSeconds=46800},ISO resolved to 06:59 of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点java.time?
以下测试用例在Java 10下完美运行:
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
class Test
{
public static void main (String[] args) throws java.lang.Exception
{
DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder().
appendPattern("EEE, dd MMM yyyy HH:mm:ss zzz").
toFormatter();
Instant result = dateFormatter.parse("Sat, 29 Sep 2018 20:49:02 GMT", Instant::from);
System.out.println("Result: " + result);
}
}
Run Code Online (Sandbox Code Playgroud)
但在Java 11下,我得到:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sat, 29 Sep 2018 20:49:02 GMT' could not be parsed at index 0
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at Test.main(Test.java:13)
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
UPDATE:更换toFormatter()与 …
我想代表一个“营业日期”,例如“2019 年 6 月 3 日”发生的交易。为此,我们主动忽略时区,因为我们完全知道日本的“2019 年 6 月 3 日”可能是美国的“2019 年 6 月 2 日”——而在“一天”内订购同样无关紧要。所有日期都是今天或之前的日期。
我显而易见的答案是,这是一个LocalDate. 但是,其他人建议将其表示为Instantof更好2019-06-03T00:00:00.000Z。
除了在 UI 中将其转换/格式化为人类可读的日期时我们必须进行的不同调用,这两种方法之间实际上有什么区别吗?
这是一个与Instant 和 LocalDateTime 之间有什么区别的不同问题?因为时间与这个问题无关,它只与过去(或当前)日期有关。
In the Java 8 DateTime API, there are 2 ways to format a date that at first glance seem to do the same thing:
DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm").format(LocalDateTime.now());
LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
Run Code Online (Sandbox Code Playgroud)
These both return a String representing the date and time at runtime.
Is there a significant difference between these 2?
不久前,我回答了一个关于如何从ZonedDateTime解析的String.
它一般有效,但在 OP 的系统和我的系统上有相同代码的不同输出,不知何故不再让我离开。
相关问题询问如何ZoneId从 a获取a ZonedDateTime,我提供了一种方法。诚然,这不是公认的答案,但似乎仍然值得某人点赞。
我的答案的特别之处在于'z'用于解析时间的模式中的String。有一个时区名称,用(澳大利亚中部标准时间)String表示时区。"Australia/Adelaide""... ACST"
当我解析它在我的系统和打印上/格式化ZonedDateTime使用DateTimeFormatter.ISO_ZONED_DATE_TIME,它打印的时间"America/Manaus"和已经提取ZoneId,它仍然是一个来自南美。OP 在我的回答下方的评论中指出,在他的系统上,至少有一个输出行显示了所需的/正确的ZoneId.
这怎么可能?系统默认语言环境对解析'z'in datetime有什么影响String吗?
这是我在问题中的回答的代码加上我的输出ZoneId.systemDefault():
public static void main(String args[]) throws Exception {
String time = "2 Jun 2019 03:51:17 PM ACST";
String pattern = "d MMM yyyy hh:mm:ss a z"; // z detects the time zone (ACST here) …Run Code Online (Sandbox Code Playgroud) java-time ×10
java ×8
datetime ×3
date ×2
time ×2
android ×1
date-parsing ×1
iso ×1
java-11 ×1
java-8 ×1
jax-rs ×1
jersey ×1
jersey-2.0 ×1
timezone ×1
translation ×1