java.time

java 8 新特性 时间api使用实例

烈酒焚心 提交于 2019-12-25 14:59:08
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> java 8 提供的 时间api java.time 比原来的Calendar 方便很多。主要提供了 日期类:LocalDate 时间类:LocateDateTime 时间戳操作类:Instant 时间持续时间计算:Duration 时区:ZoneId 日期间隔:Period java 8 api 是整合了原第三方工具(org.joda.time),使用将会更加方便。 以下实例针对,日常使用的时间计算方式,进行实例验证。可以根据自己需要提取,时间工具类。 以下是具体的使用实例,建议大家都转换新的时间方式,虽然中间可能要花费些时间,但个人感觉还是值得。 public static void main(String[] args) { Instant first = Instant.now(); // Current Time LocalTime time = LocalTime.now(); System.out.println("Current Time=" + time); // Creating LocalTime by providing input arguments LocalTime specificTime = LocalTime.of(12, 20, 25, 40); System.out

Java - Parse date with optional seconds [duplicate]

一曲冷凌霜 提交于 2019-12-25 03:17:54
问题 This question already has answers here : How do I simply parse a date without a year specified? (5 answers) DateTimeFormatter could not be parsed using “HH:mm E d MMM YYYY” pattern (2 answers) Closed last year . Given this date I want to parse: 15th Dec 16:00 +01:00 with this code Map<Long, String> ordinalNumbers = new HashMap<>(42); ordinalNumbers.put(1L, "1st"); ordinalNumbers.put(2L, "2nd"); ordinalNumbers.put(3L, "3rd"); ordinalNumbers.put(21L, "21st"); ordinalNumbers.put(22L, "22nd");

DateTimeFormatter create pattern [duplicate]

依然范特西╮ 提交于 2019-12-25 01:23:21
问题 This question already has answers here : How to get start and end range from list of timestamps? (2 answers) Converting ISO 8601-compliant String to java.util.Date (28 answers) Closed 6 months ago . I have this date: 2008-01-10T11:00:00-05:00 (date, T(separator), time, offset) I have this: DateTimeFormatter.ofPattern("yyyy-MM-ddSEPARATORHH-mm-ssOFFSET") I use this table to create my pattern. But, I don't find how to note SEPARATOR (T) and OFFSET. For OFFSET exists this: x zone-offset offset-x

DateTimeFormatter and SimpleDateFormat produce different strings [duplicate]

删除回忆录丶 提交于 2019-12-24 06:29:59
问题 This question already has answers here : Why when year is less than 1884, it remove few milliseconds? (2 answers) Closed 7 months ago . This is not a duplicate as some people think. It is about two standard Java classes for formatting dates that produce different strings for the same value of milliseconds since the epoch. For values of milliseconds since the epoch that occur before some point in the year 1883, SimpleDateFormat and DateTimeFormatter will produce different results. For reasons

How to get the current local time or system time in spark-scala dataframe?

僤鯓⒐⒋嵵緔 提交于 2019-12-11 19:35:57
问题 I am trying to get the local time in spark-scala but it is returning UTC. I am using java.time.LocalDateTime to get the current timestamp. But its returning the UTC standard. java.sql.Timestamp.valueOf(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss.SSSSSS").format(LocalDateTime.now)) The LocalDateTime is returning local time in spark shell, but in my code it is giving UTC standard. val time: LocalDateTime = LocalDateTime.now How to get the current time? The current output is UTC. I need the

Java Instant.parse on Date java 8

久未见 提交于 2019-12-10 21:53:04
问题 I have some legacy KML documents which includes a time stamp entry. Why is the below date not valid when using Instant to parse? Both methods are suppose to parse ISO 8601 formatted dates. String dateString = "2017-12-04T08:06:60Z" Using java.time.Instant.parse(dateString) throws an error "DateTimeParseException Text 2017-12-04T08:06:60Z could not be parsed at index 0." However, when using Date myDate = javax.xml.bind.DatatypeConverter.parseDateTime( dateString ) myDate is parsed correctly...

What is the equivalent of Calendar.roll in java.time?

我的未来我决定 提交于 2019-12-01 22:29:32
I was studying the old Calendar API to see how bad it was, and I found out that Calendar has a roll method. Unlike the add method, roll does not change the values of bigger calendar fields. For example, the calendar instance c represents the date 2019-08-31. Calling c.roll(Calendar.MONTH, 13) adds 13 to the month field, but does not change the year, so the result is 2019-09-30. Note that the day of month changes, because it is a smaller field. Related I tried to find such a method in the modern java.time API. I thought such a method has to be in LocalDate or LocalDateTime , but I found nothing