java-time

How to get previous month and years in java?

若如初见. 提交于 2019-11-26 16:49:02
问题 How do I find out the last month and its year in java e.g. If today is 10th oct 2012, result should be Month = 9 and year = 2012, If today is 10th jan 2013, result should be Month = 12 and year = 2012 回答1: Your solution is here but instead of addition you need to use subtraction c.add(Calendar.MONTH, -1); Then you can call getter on the Calendar to acquire proper fields int month = c.get(Calendar.MONTH) + 1; // beware of month indexing from zero int year = c.get(Calendar.YEAR); 回答2: java.time

How to check if a date Object equals yesterday?

社会主义新天地 提交于 2019-11-26 16:35:43
Right now I am using this code 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 There's got to be a smoother way to check if the date returned by getdateFromLine() is yesterday's date. Only the date matters, not the time. That's why I used SimpleDateFormat. Thanks for your

Is Joda Time deprecated with java 8 Date and Time API? (java.time)

时光毁灭记忆、已成空白 提交于 2019-11-26 16:30:51
问题 Is there any reason to use Joda Time if I can use Java 8 Date and Time API ( java.time )? Should I use Java 8 Date and Time every time? 回答1: The official statement of the author of Joda-time himself is to migrate as soon as Java-8 is available . See also this citation from the website: Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310). So the short answer to your question is: YES

Can't parse String to LocalDate (Java 8)

五迷三道 提交于 2019-11-26 16:22:43
问题 My input is a String representation of a date in the format "01-07-2015" for July 1, 2015. I'm trying to parse this into a java.time.LocalDate variable: final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd-MM-YYYY"); final String input = "01-07-2015"; final LocalDate localDate = LocalDate.parse(input, DATE_FORMAT); Based on the DateTimeFormatter JavaDoc, I would expect this to work. However, I'm greeted with a very friendly and helpful message: Caused by: java.time

Java 8 LocalDate - How do I get all dates between two dates?

此生再无相见时 提交于 2019-11-26 16:03:56
问题 Is there a usablility to get all dates between two dates in the new java.time API? Let's say I have this part of code: @Test public void testGenerateChartCalendarData() { LocalDate startDate = LocalDate.now(); LocalDate endDate = startDate.plusMonths(1); endDate = endDate.withDayOfMonth(endDate.lengthOfMonth()); } Now I need all dates between startDate and endDate . I was thinking to get the daysBetween of the two dates and iterate over: long daysBetween = ChronoUnit.DAYS.between(startDate,

Differences between Java 8 Date Time API (java.time) and Joda-Time

风流意气都作罢 提交于 2019-11-26 15:36:12
I know there are questions relating to java.util.Date and Joda-Time. But after some digging, I couldn't find a thread about the differences between the java.time API (new in Java 8 , defined by JSR 310 ) and Joda-Time . I have heard that Java 8’s java.time API is much cleaner and can do much more than Joda-Time. But I cannot find examples comparing the two. What can java.time do that Joda-Time cannot? What can java.time do better than Joda-Time? Is the performance better with java.time? Common features a) Both libraries use immutable types. Joda-Time also offers additional mutable types like

Getting the date from a ResultSet for use with java.time classes

末鹿安然 提交于 2019-11-26 14:07:33
问题 Is there anyway to get a java.time (new in Java 8) compatible time class out of a ResultSet ? I am aware you can use ResultSet 's getDate or getTimestamp but these method return java.sql.Date / java.sql.Timestamp objects which are now deprecated so it seems like bad practice to use them in order to create a ZonedDateTime or similar. 回答1: Most database vendors don't support JDBC 4.2 yet. This specification says that the new java.time -types like LocalDate will/should be supported using the

Why does the new Java 8 Date Time API not have nanosecond precision? [duplicate]

爷,独闯天下 提交于 2019-11-26 13:50:01
问题 This question already has answers here : Java 8 Instant.now() with nanosecond resolution? (4 answers) Closed 3 years ago . One of the features of the new Date Time API in Java 8 is supposed to be nanosecond precision. However when I print the current Date Time to the console like so DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd'T'HH:mm:ss,nnnnnnnnnZ"); System.out.println(OffsetDateTime.now().format(formatter)); I only see millisecond precision: 2015-11-02T12:33:26

How to extract epoch from LocalDate and LocalDateTime?

点点圈 提交于 2019-11-26 13:09:47
问题 How do I extract the epoch value to Long from instances of LocalDateTime or LocalDate ? I've tried the following, but it gives me other results: LocalDateTime time = LocalDateTime.parse("04.02.2014 19:51:01", DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")); System.out.println(time.getLong(ChronoField.SECOND_OF_DAY)); // gives 71461 System.out.println(time.getLong(ChronoField.EPOCH_DAY)); // gives 16105 What I want is simply the value 1391539861 for the local datetime "04.02.2014 19:51:01"

JPA support for Java 8 new date and time API

a 夏天 提交于 2019-11-26 13:00:47
I'm using Java 8 for my new project. I'm trying to use new date and time api in java 8 however I don't know if JPA 2.1 fully supports this new Date and Time API or not. Please share your experience/opinion in JPA`s supports for new date and time API in Java 8. Can I use new date and time api in Java 8 safely with JPA 2.1? UPDATE: I'm using Hibernate (4.3.5.Final) as JPA implementation. JPA 2.1 is a spec that came out before Java 1.8, so doesn't mandate any support for it. Obviously some implementations may support some Java 1.8 features. Some have problems with Java 1.8 bytecode (e.g