给定a FileTime fileTime,如何以自定义方式格式化为字符串?
String s = fileTime.toString() 仅以ISO格式提供.
String s = DateTimeFormatter.ofPattern("uuuu-MMM-dd HH:mm:ss")
.format(fileTime.toInstant());
Run Code Online (Sandbox Code Playgroud)
投 UnsupportedTemporalTypeException: Unsupported field: Year
使用Joda Time,您可以做一些非常酷的事情,例如:
package temp;
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
import org.joda.time.DateTimeFieldType;
public class TestDateTimeComparator {
public static void main(String[] args) {
//Two DateTime instances which have same month, date, and hour
//but different year, minutes and seconds
DateTime d1 = new DateTime(2001,05,12,7,0,0);
DateTime d2 = new DateTime(2014,05,12,7,30,45);
//Define the lower limit to be hour and upper limit to be month
DateTimeFieldType lowerLimit = DateTimeFieldType.hourOfDay();
DateTimeFieldType upperLimit = DateTimeFieldType.monthOfYear();
//Because of the upper and lower limits , the comparator shall only consider …Run Code Online (Sandbox Code Playgroud) 在Java-8之前,我习惯于始终保持与Epoch相关的任何日期/时间相关的毫秒数,并且只能在出路时处理人类可读的日期/时间,即在UI或日志文件中,或在解析用户生成的输入时.
我认为这对Java-8来说仍然是安全的,现在我正在寻找一种最简洁的方法来获得一个毫秒时间戳的格式化日期.我试过了
df = Dateformatter.ofPattern("...pattern...");
df.format(Instant.ofEpochMilli(timestamp))
Run Code Online (Sandbox Code Playgroud)
但炸弹出与Unsupported field: YearOfEra在Instant.getLong(...)我一半懂.现在用什么而不是Instant?
LocalDateTime.ofEpoch(Instant, ZoneId)似乎错了,因为我不在乎当地的时间.我只是想在应用格式化程序时看到本地时区.在内部它应该只是Instant.
同样的ZonedDateTime.ofInstant(Instant, ZoneId),我想ZoneId只在格式化时应用.但是我注意到它DateTimeFormatter本身不再与时区有关,似乎,所以我认为我需要使用上述之一.
哪个是首选,为什么?或者我应该使用另一种方法将epoch-millis时间戳格式化为带时区的日期/时间?
为什么这个测试通过,而月份值显然是无效的(13)?
@Test
public void test() {
String format = "uuuuMM";
String value = "201713";
DateTimeFormatter.ofPattern(format).withResolverStyle(ResolverStyle.STRICT)
.parse(value);
}
Run Code Online (Sandbox Code Playgroud)
使用时态查询时,会DateTimeParseException抛出预期:
DateTimeFormatter.ofPattern(format).withResolverStyle(ResolverStyle.STRICT)
.parse(value, YearMonth::from);
Run Code Online (Sandbox Code Playgroud)
没有TemporalQuery指定时会发生什么?
编辑:13值似乎是一个特殊的值,因为我学到了ΦXocę웃Пepeúpaツ的答案(见Undecimber).
但是,即使使用其他值,例如50,也不会抛出异常:
@Test
public void test() {
String format = "uuuuMM";
String value = "201750";
DateTimeFormatter.ofPattern(format).withResolverStyle(ResolverStyle.STRICT)
.parse(value);
}
Run Code Online (Sandbox Code Playgroud) 我刚刚根据新的(ish)java 8时间包将我的许多日期转换为LocalDateTime.到目前为止,我一直很喜欢切换,直到我开始尝试序列化和反序列化.
如何配置Jackson支持他们?:
LocalDateTime --serialize - > UTC Timestamp --deserialize - > LocalDateTime?
这里有很多关于转换为格式化字符串的材料,但我似乎无法找到utc时间戳的开箱即用解决方案.
当我使用时SimpleDateFormat,它可以解析.
SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
format.setLenient(false);
Date d = format.parse(date);
Run Code Online (Sandbox Code Playgroud)
但是当我使用Java 8 DateTimeFormatter时
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
LocalDate localDate = LocalDate.parse(date, formatter);
Run Code Online (Sandbox Code Playgroud)
它抛出
java.time.format.DateTimeParseException:文本 '201510' 无法解析:无法从TemporalAccessor获得LOCALDATE:{年份= 2015,MonthOfYear = 10},类型的Java的ISO .time.format.Parsed
日期的字符串值是"201510".
我的代码中有"ddMMyy"模式,我使用appendValue方法指定了它:
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendValue(ChronoField.YEAR_OF_ERA, 2)
.toFormatter();
System.out.println(LocalDate.parse("100199", dateTimeFormatter));
Run Code Online (Sandbox Code Playgroud)
但是这会产生"0099"年份:
0099-01-10
如果我将其更改为使用appendPattern:
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.appendPattern("ddMMyy")
.toFormatter();
System.out.println(LocalDate.parse("100199", dateTimeFormatter));
Run Code Online (Sandbox Code Playgroud)
我有正确的结果,一年"2099"与世纪.
2099年1月10日
代码对我来说似乎相同,为什么它不能产生相同的结果?为什么在第一种情况下缺少这个世纪?
我的代码如下所示:
String PROPER_DATE_FORMAT = "yyyy-MM-dd";
String format = "yyyy-MM-dd'T'HH:mm:ss.SSSX";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = sdf.parse(Instant.now().toString());
SimpleDateFormat properFormat = new SimpleDateFormat(PROPER_DATE_FORMAT);
String formatterDate = properFormat.format(d);
Run Code Online (Sandbox Code Playgroud)
现在,大多数时候,这段代码可以工作,但时不时地,我会返回 Instant.now() ,2018-05-25T18:56:09Z然后它会抛出一个异常,因为格式不匹配。
为什么有时会不一致?
例外的是java.lang.RuntimeException: java.text.ParseException: Unparseable date: "2018-05-25T18:56:09Z"
通常,whenInstant.now().toString()会返回一个具有上述格式的字符串。同样,这段代码在大多数情况下都有效,但也有几次不起作用,我很困惑为什么。
我想是否是因为末尾有一个零但没有显示,但我不相信,因为我能够通过 Instant. 获得 2018-05-25T20:06:58.900Z 的值。 now().toString()
我想得到以秒为单位的差异,以确定系统时区是领先还是落后于远程时区。这里的远程时区值是我从数据库获取的“GMT”。它可能是“美国/东部”,我将其转换为“美国/纽约”。但对于 GMT,我收到错误。
ZoneId.systemDefault().getRules().getOffset(Instant.now()).getTotalSeconds()
- ZoneId.of("GMT").getRules().getOffset(Instant.now()).getTotalSeconds()
Run Code Online (Sandbox Code Playgroud)
但它给出了以下错误,
Exception in thread "main" java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format:
at java.time.ZoneOffset.of(Unknown Source)
at java.time.ZoneId.of(Unknown Source)
at java.time.ZoneId.of(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
如何解决这个错误?用什么来代替 GMT?
我正在使用以下 ISO8601 格式:
YYYY-MM-DDThh:mm:ssZ
Run Code Online (Sandbox Code Playgroud)
我曾经OffsetDateTime.parse()解析过这种格式。我能够通过在此处传递t(而不是T) 和z(而不是Z) 来解析日期时间。
那么谁能告诉它在ISO8601中是否允许,还是仅在解析逻辑中被忽略?