相关疑难解决方法(0)

java.text.ParseException:无法解析的日期:java.text.DateFormat.parse(DateFormat.java:579)

我有问题SimpleDateFormat

SimpleDateFormat dtfmt=new SimpleDateFormat("dd MMM yyyy hh:mm a", Locale.getDefault());
Date dt=dtfmt.parse(deptdt);
Run Code Online (Sandbox Code Playgroud)

在 Android 模拟器中工作正常,但在手机中出现此错误:

W/System.err: java.text.ParseException: Unparseable date: "24 Oct 2016 7:31 pm" (at offset 3) W/System.err: at java.text.DateFormat.parse(DateFormat.java:579)

有什么解决办法吗?

java android date datetime-parsing unparseable

2
推荐指数
1
解决办法
1211
查看次数

DateTimeFormatter基于周的年份差异

我正在将我的应用程序从Joda-Time迁移到Java 8 java.time.

我遇到的一件事是使用图案中的模式打印基于周的一年DateTimeFormatter.

注意:我已经看到了这个问题: Java Time使用DateTimeFormatter解析基于星期的周模式

根据文件

y       year-of-era                 year              2004; 04
Y       week-based-year             year              1996; 96
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试这两个时,似乎Y总是和它一样y.

我的测试代码:

DateTimeFormatter yearF = DateTimeFormatter.ofPattern("yyyy").withZone(ZoneOffset.UTC);
DateTimeFormatter weekYearF = DateTimeFormatter.ofPattern("YYYY").withZone(ZoneOffset.UTC);

DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
    .appendValue(ChronoField.YEAR_OF_ERA)   .appendLiteral(" ") .append(yearF)
    .appendLiteral(" -- ")
    .appendValue(IsoFields.WEEK_BASED_YEAR) .appendLiteral(" ") .append(weekYearF)
    .toFormatter()
    .withZone(ZoneOffset.UTC);

System.out.println(dateTimeFormatter.toString());

ZonedDateTime dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(946778645000L), ZoneOffset.UTC);
for (int i = 2000 ; i < 2020; i ++ ) {
    System.out.println(dateTime.withYear(i).format(dateTimeFormatter));
}
Run Code Online (Sandbox Code Playgroud)

输出:

Value(YearOfEra)' '(Value(YearOfEra,4,19,EXCEEDS_PAD))' -- 'Value(WeekBasedYear)' '(Localized(WeekBasedYear,4,19,EXCEEDS_PAD)) …
Run Code Online (Sandbox Code Playgroud)

java datetime dayofweek java-8 java-time

2
推荐指数
1
解决办法
848
查看次数

在java中将字符串格式化为日期

我想比较 WebElements 日期以验证排序是否正确。然而,日期的值例如如下:“2021年4月5日12:30pm”、“2018年10月22日09:18am”、“2015年2月1日11:36pm”、

我尝试了下面的代码,但它返回 1970 作为日期,并且在日期为 2 位数字的情况下会出现错误:


DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MMMM d yyyy HH:mma", Locale.US);
LocalDate date = LocalDate.parse(dt, dateFormatter);

// or

Date sdf = new SimpleDateFormat("MMMM d u hh:mma").parse(dt);



Run Code Online (Sandbox Code Playgroud)

java datetime simpledateformat datetimeformatter

2
推荐指数
1
解决办法
199
查看次数