In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "detailed introduction of java long type storage timestamp". In daily operation, I believe many people have doubts about the detailed introduction of java long type storage timestamp. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "detailed introduction of java long type storage timestamp". Next, please follow the editor to study!
The storage time is intended to be stored with a timestamp, and the long type is intended to represent the timestamp, but there is a question when using the long type to store the timestamp.
When writing a unit test, a long type is used to store the timestamp and it is found that the compiler has reported an error
At first, I guessed that it was because the long was not big enough to store. Then use the double type to save:
Found that the wrong report, think about it is wrong, the amount of double storage should be very large, will not even be unable to store timestamps.
After adding the decimal point, it can be saved:
After adding the decimal point, you can save it. Think about it carefully. Before adding the decimal point, he is an integer. After adding the decimal point, he becomes a floating point. The timestamp before the addition of the decimal point is a plastic literal value. Plus the literal value of the decimal point is a floating point. The previous error report should not be a storage type problem, but the literal value range is out of range.
Test with a string:
Change the timestamp to the literal value of a string, parse it into long and int, and then output them:
It was found that the first long type was successfully output, while the int type reported a number error.
This shows that long can store timestamps, while int cannot store timestamps, so judging that they cannot be stored before is out of range because the literal value is shaping.
Java various date / time objects converted to Long timestamps
Package cn.xbz; import java.text.SimpleDateFormat;import java.time.*;import java.time.format.DateTimeFormatter;import java.util.Calendar;import java.util.Date; / * @ title various date / time objects turn to timestamps * @ author Xingbz * @ createDate 2018-5-18 * / public class DateTime2MillisDemo {private static final String FORMAT_STR = "yyyy-MM-dd HH:mm:ss" Public static void main (String [] args) {System.out.println ("= JDK7 and before ="); Long L1 = getMillis1 (); Long L2 = date2Millis (new Date ()); Long L3 = calendar2Millis (Calendar.getInstance ()); Long L4 = string2Millis (new SimpleDateFormat (FORMAT_STR) .format (new Date (), FORMAT_STR)) / in order to be consistent with the above System.out.println (L1 + "\ n" + L2 + "\ n" + L3 + "\ n" + L4); / / there will be millisecond differences between System.out.println ("= JDK8 ="); Long L5 = getMillis2 (); Long L6 = localDateTime2Millis (LocalDateTime.now ()); Long L7 = localDate2Millis (LocalDate.now ()); Long L8 = clock2Millis (Clock.systemUTC ()); Long L9 = zoneDateTime2Millis (ZonedDateTime.now ()) Long L10 = string2MillisWithJDK8 (LocalDateTime.now (). Format (DateTimeFormatter.ofPattern (FORMAT_STR)), FORMAT_STR); / / to be consistent with the above System.out.println (L5 + "\ n" + L6 + "\ n" + L7 + "\ n" + L8 + "\ n" + L9 + "\ n" + L10) / / there will be a difference of several milliseconds} / * JDK7 and before * / / * * get timestamp * / public static Long getMillis1 () {return System.currentTimeMillis ();} / * * Date to timestamp * / public static Long date2Millis (Date date) {return date.getTime ();} / * * Calendar to timestamp * / public static Long calendar2Millis (Calendar calendar) {return calendar.getTime (). GetTime () } / * * date string turns timestamp * / public static Long string2Millis (String dateStr, String formatStr) {try {SimpleDateFormat simpleDateFormat = new SimpleDateFormat (formatStr); return simpleDateFormat.parse (dateStr). GetTime ();} catch (Exception e) {return 0L;}} / * JDK8 * / * * get timestamp * / public static Long getMillis2 () {return Instant.now () .toEpochMilli () } / * * LocalDateTime to timestamp * / public static Long localDateTime2Millis (LocalDateTime localDateTime) {return localDateTime.toInstant (ZoneOffset.ofHours (8)). ToEpochMilli ();} / * LocalDate to timestamp * / public static Long localDate2Millis (LocalDate localDate) {return LocalDateTime.of (localDate, LocalTime.MIN) .toInstant (ZoneOffset.ofHours (8)). ToEpochMilli ();} / * Clock to timestamp * / public static Long clock2Millis (Clock clock) {return clock.millis () } / * * ZoneDateTIme to timestamp (this is not commonly used) * / public static Long zoneDateTime2Millis (ZonedDateTime zonedDateTime) {return zonedDateTime.toLocalDateTime (). ToInstant (ZoneOffset.ofHours (8)). ToEpochMilli (); / /! How laborious} / * * String to timestamp (JDK8) * / public static Long string2MillisWithJDK8 (String dateStr, String formatStr) {return LocalDateTime.parse (dateStr, DateTimeFormatter.ofPattern (formatStr)) .toInstant (ZoneOffset.ofHours (8)). ToEpochMilli ();}}
The output is as follows:
At this point, the study of "detailed introduction to java long type storage timestamp" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.