我有几毫秒.我需要它转换为日期格式
例:
23/10/2011
怎么实现呢?
我有以下实体类:
@Entity
public class Event {
private OffsetDateTime startDateTime;
// ...
}
Run Code Online (Sandbox Code Playgroud)
然而,持续的,然后从与JPA 2.2结果数据库读取的实体/在信息丢失:所述ZoneOffset的startDateTime改变UTC(在ZoneOffset所使用的数据库的时间戳).例如:
Event e = new Event();
e.setStartDateTime(OffsetDateTime.parse("2018-01-02T09:00-05:00"));
e.getStartDateTime().getHour(); // 9
e.getStartDateTime().getOffset(); // ZoneOffset.of("-05:00")
// ...
entityManager.persist(e); // Stored startDateTime as timestamp 2018-01-02T14:00Z
Event other = entityManager.find(Event.class, e.getId());
other.getStartDateTime().getHour(); // 14 - DIFFERENT
other.getStartDateTime().getOffset(); // ZoneOffset.of("+00:00") - DIFFERENT
Run Code Online (Sandbox Code Playgroud)
我需要使用OffsetDateTime:我无法使用ZonedDateTime,因为区域规则发生了变化(而且还会受到此信息丢失的影响).我无法使用LocalDateTime,因为Event世界上任何地方都会发生这种情况,因为ZoneOffset出于准确性原因我需要原件.我无法使用,Instant因为用户填写了事件的开始时间(事件就像约会).
要求:
需要能够>, <, >=, <=, ==, …
我有一个SpringBoot应用程序.使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件.
我有一个具有2个属性的域对象(initDate,endDate).我想创建2个转换器来处理mySQL DB
@Convert(converter = LocalDateTimeAttributeConverter.class)
private LocalDateTime initDate;
@Convert(converter = ZonedDateTimeAttributeConverter.class)
private ZonedDateTime endDate;
Run Code Online (Sandbox Code Playgroud)
转换器1(可以)
@Converter
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
return (localDateTime == null ? null : Timestamp.valueOf(localDateTime));
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我想要创建的那个
@Converter
public class ZonedDateTimeAttributeConverter implements AttributeConverter<ZonedDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(ZonedDateTime zoneDateTime) {
return (zoneDateTime == null ? null : Timestamp.valueOf(zoneDateTime));
} …Run Code Online (Sandbox Code Playgroud) 我有Entry对象列表.Entry是一个:
class Entry {
private final Date date;
private final String value;
// constructor
// getters
}
Run Code Online (Sandbox Code Playgroud)
我需要在白天对这些条目进行分组.例如,
2011-03-21 09:00 VALUE1
2011-03-21 09:00 VALUE2
2011-03-22 14:00 VALUE3
2011-03-22 16:00 VALUE4
2011-03-21 16:00 VALUE5
Run Code Online (Sandbox Code Playgroud)
应分组:
2011-03-21
VALUE1
VALUE2
VALUE5
2011-03-22
VALUE3
VALUE4
Run Code Online (Sandbox Code Playgroud)
我想得到一个Map<Date, List<Entry>>.如何使用Stream API(groupingBy collector)获取此信息?
我的尝试如下:
final Map<Date, List<Entry>> entries =
list.stream().collect(Collectors.groupingBy(request -> {
final Calendar ogirinal = Calendar.getInstance();
ogirinal.setTime(request.getDate());
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, ogirinal.get(Calendar.DAY_OF_MONTH));
cal.set(Calendar.MONTH, ogirinal.get(Calendar.MONTH));
cal.set(Calendar.YEAR, ogirinal.get(Calendar.YEAR)); …Run Code Online (Sandbox Code Playgroud) 我的eta值是OffsetDateTime,我有一个DateDate类型的scheduledDate.如果没有设置eta,我想回到日期.
这个日期的一个例子是Tue Jul 21 10:32:28 PDT 2020.要转换这个问题,我想这样做:
OffsetDateTime.ofInstant(dto.getScheduledTime().ToInstant(), ZoneOffset.UTC)
感觉就像UTC偏移量是错误的,因为日期中有PDT了,但同时这也是不喜欢"美国/洛杉矶"一timezoneId.
我对如何处理这个问题感到有些困惑.
灵感来自以下帖子
我需要一个函数来返回该月中给定日期的序数位置,例如:
01/01/1970 = 1 因为这是1970年1月的第一个星期四
02/01/1970 = 1 因为这是1970年1月的第一个星期五
1970年1月19日 = 3 因为这是1970年1月的第三个星期一
1970年1月31日 = 5 因为这是1970年1月的第五个星期六
我试过了什么? - 没什么......我甚至不知道从哪里开始; Java 8日期/时间API对我来说是一个新手.
理想情况下,我想要一个具有此签名的函数:
public int getOrdinalPosition(TemporalAccessor temporal) {
...
}
Run Code Online (Sandbox Code Playgroud) 在Java 8 Date Time API中,我将使用DateTimeFormatter以下API 打印时间:
DateTimeFormatter timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL);
LocalTime time = LocalTime.of(12, 45, 0);
System.out.println(timeFormatter.format(time));
Run Code Online (Sandbox Code Playgroud)
FormatStyle.FULL- 这种格式样式适用于LocalDate和LocalDateTime实例.但是在LocalTime实例中抛出异常:
java.time.DateTimeException: Unable to extract value: class java.time.format.DateTimePrintContext$1
Run Code Online (Sandbox Code Playgroud)
根据文件:
public enum FormatStyle {
// ordered from large to small
/**
* Full text style, with the most detail.
* For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.
*/
FULL,
Run Code Online (Sandbox Code Playgroud)
为什么会抛出异常?
我试图像这样得到当月的一周:
YearMonth
.from(Instant.now().atZone(ZoneId.of("UTC")))
.get(WeekFields.ISO.weekOfMonth())
Run Code Online (Sandbox Code Playgroud)
但这引发了
java.time.temporal.UnsupportedTemporalTypeException:不支持的字段:DayOfWeek
我似乎无法解决为什么我得到这个例外,因为我没有做任何事情DayOfWeek.有任何想法吗?
我的要求是根据一组指定的有效格式来验证日期字符串的格式正确。
有效格式:
MM/dd/yy
MM/dd/yyyy
Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的测试方法,该方法使用Java 8 DateTimeFormatterBuilder来创建支持多种可选格式的灵活格式化程序。这是代码:
public static void test() {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yy"))
.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yyyy"))
.toFormatter();
String dateString = "10/30/2017";
try {
LocalDate.parse(dateString, formatter);
System.out.println(dateString + " has a valid date format");
} catch (Exception e) {
System.out.println(dateString + " has an invalid date format");
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此,这里是输出
10/30/2017 has an invalid date format
Run Code Online (Sandbox Code Playgroud)
如您在代码中看到的,有效的日期格式是MM / dd / yy和MM / dd / yyyy。我的期望是日期10/30/2017应该有效,因为它与MM / dd / yyyy相匹配。但是,2017年10月30日被报告为无效。
怎么了?为什么这不起作用?
我也试过
.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yy[yy]"))
Run Code Online (Sandbox Code Playgroud)
代替
.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yy"))
.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yyyy"))
Run Code Online (Sandbox Code Playgroud)
但仍然存在相同的问题。
如果使用以下代码,此代码将按预期运行:
String …Run Code Online (Sandbox Code Playgroud) 我现在正在巴黎运行这段代码,其中12:40,其中ECT = ECT - 欧洲/巴黎
LocalDateTime creationDateTime = LocalDateTime.now(Clock.systemUTC());
ZoneId zone = ZoneId.of(ZoneId.SHORT_IDS.get("ECT"));
System.out.println ("creationDateTime --------------------------> " + creationDateTime);
System.out.println ("creationDateTime.atZone(zone).getHour() ---> " + creationDateTime.atZone(zone).getHour());
System.out.println ("creationDateTime.atZone(zone).getMinute() -> " + creationDateTime.atZone(zone).getMinute());
Run Code Online (Sandbox Code Playgroud)
但我在控制台得到这个
creationDateTime --------------------------> 2017-05-16T10:40:07.882
creationDateTime.atZone(zone).getHour() ---> 10
creationDateTime.atZone(zone).getMinute() -> 40
Run Code Online (Sandbox Code Playgroud)
我不应该得到12:40 ???????