如何从当前日期减少一个月,并希望java.util.Date使用此代码在变量im中疼痛,但它在第二行显示错误
java.util.Date da = new Date();
da.add(Calendar.MONTH, -1); //error
Run Code Online (Sandbox Code Playgroud)
如何将这个日期存储在java.util.Date变量中?
该java.util.Date班有一个名为方法toInstant()的转换Date实例到java.time.Instant.
在java.sql.Date类扩展了java.util.Date类,但当我尝试调用toInstant()上java.sql.Date,我收到一个UnsupportedOperationException.
为什么toInstant()不支持操作java.sql.Date?
什么是"正确"的方式到转换java.sql.Date到java.time.Instant?
什么是LocalDate从Java 8和XMLGregorianCalendar?之间进行转换的最佳方法?
给定LocalDate时如何获得当天的结束?
我可以做到这一点
LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59));
Run Code Online (Sandbox Code Playgroud)
但是在一天结束时是否有相同的'atStartOfDay'方法?
LocalDate.now().atStartOfDay();
LocalDate.now().atEndOfDay(); //doesn't work
Run Code Online (Sandbox Code Playgroud) 现在我正在使用此代码
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE) - 1, 12, 0, 0); //Sets Calendar to "yeserday, 12am"
if(sdf.format(getDateFromLine(line)).equals(sdf.format(cal.getTime()))) //getDateFromLine() returns a Date Object that is always at 12pm
{...CODE
Run Code Online (Sandbox Code Playgroud)
必须有一种更平滑的方法来检查getdateFromLine()返回的日期是否是昨天的日期.只有日期很重要,而不是时间.这就是我使用SimpleDateFormat的原因.感谢您的帮助!
DateTimeFormmater 似乎没有处理该月的单个数字日:
String format = "MM/dd/yyyy";
String date = "5/3/1969";
System.out.println(new SimpleDateFormat(format).parse(date));
System.out.println(LocalDate.parse(date, DateTimeFormatter.ofPattern(format)));
Run Code Online (Sandbox Code Playgroud)
在此示例中,SimpleDateFormat正确解析日期,但DateTimeFormatter会引发异常.如果我使用零填充日期,例如"05/03/1969",两者都有效.但是,如果月中的某一天或一年中的DateTimeFormatter某个月是单个数字,则抛出异常.
DateTimeFormatter解析每月和每月的一位和两位数的正确格式是什么?
我有一个很长的时间戳1499070300(相当于周一,2017年7月3日16:25:00 +0800)但是当我将它转换为LocalDateTime时,我得到1970-01-18T16:24:30.300
这是我的代码
long test_timestamp = 1499070300;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
.getDefault().toZoneId());
Run Code Online (Sandbox Code Playgroud) 我正在尝试将Spring Data JPA 1.8与Java 8 Date/Time API JSR-310一起使用.
一切似乎都有效,直到我试图让所有车辆在两个LocalDateTimes之间.返回的实体数量似乎只与它应该的数量松散相关.
@Repository
public interface VehicleRepository extends JpaRepository<Vehicle, Long> {
List<Vehicle> findByDateTimeBetween(LocalDateTime begin, LocalDateTime end);
}
Run Code Online (Sandbox Code Playgroud)
@Entity
@Table(name = "VEHICLE")
public class Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "IDX", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.AUTO)
private long vehicleId;
@Column(name = "DATE_TIME", nullable = false)
private LocalDateTime dateTime = LocalDateTime.now();
// Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.8.0.RELEASE</version>
</dependency> …Run Code Online (Sandbox Code Playgroud) 所以我希望这个代码能够在新的Java 8日期/时间包下工作,因为它所做的只是将给定的ZonedDateTime转换为字符串并使用相同的内置DateTimeFormatter实例(ISO_INSTANT)返回:
ZonedDateTime now = ZonedDateTime.now();
System.out.println(ZonedDateTime.parse(
now.format(DateTimeFormatter.ISO_INSTANT),
DateTimeFormatter.ISO_INSTANT));
Run Code Online (Sandbox Code Playgroud)
但显然它没有:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2014-09-01T19:37:48.549Z' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=549, NanoOfSecond=549000000, MicroOfSecond=549000, InstantSeconds=1409600268},ISO 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)
我已经看过这个条目,但它没有帮助我,因为需要ZonedDateTime对象而不是本地对象,还因为我已经安装了8u20:无法使用Java8中的DateTimeFormatter和ZonedDateTime从TemporalAccessor获取ZonedDateTime
任何人都知道这里发生了什么?
如果我可以使用Java 8 Date and Time API(java.time),有没有理由使用Joda Time ?
我应该每次都使用Java 8日期和时间吗?