java-time

How to reduce one month from current date and stored in date variable using java?

走远了吗. 提交于 2019-11-28 19:33:09
问题 How to reduce one month from current date and want to sore in java.util.Date variable im using this code but it's shows error in 2nd line java.util.Date da = new Date(); da.add(Calendar.MONTH, -1); //error How to store this date in java.util.Date variable? 回答1: Use Calendar: Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1); Date result = cal.getTime(); 回答2: Starting from Java 8, the suggested way is to use the Date-Time API rather than Calendar . If you want a Date object to

Is there any way to convert ZoneId to ZoneOffset in java 8?

寵の児 提交于 2019-11-28 19:04:11
I have an epoch second and a zoneId,by method1.It can be convert to LocalDateTime with system default zoneId,but I don't find the way to convert epoch second to LocalDateTime by method2,because there is no ZoneOffset.systemDefault .I think it's obscure. import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset} val epochSecond = System.currentTimeMillis() / 1000 LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), ZoneId.systemDefault())//method1 LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.MAX)//method2 Here is how you can get ZoneOffset from ZoneId : Instant instant =

Java8 java.util.Date conversion to java.time.ZonedDateTime

放肆的年华 提交于 2019-11-28 18:31:42
I am getting the following exception while trying to convert java.util.Date to java.time.LocalDate . java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: 2014-08-19T05:28:16.768Z of type java.time.Instant The code is as follow: public static Date getNearestQuarterStartDate(Date calculateFromDate){ int[] quaterStartMonths={1,4,7,10}; Date startDate=null; ZonedDateTime d=ZonedDateTime.from(calculateFromDate.toInstant()); int frmDateMonth=d.getMonth().getValue(); Is there something wrong in the way I am using the ZonedDateTime class? As per documentation, this should

How to convert java.sql.timestamp to LocalDate (java8) java.time?

一曲冷凌霜 提交于 2019-11-28 17:41:22
In Java 8, how can I convert a Timestamp (in java.sql ) to a LocalDate (in java.time )? You can do: timeStamp.toLocalDateTime().toLocalDate(); I'll slightly expand @assylias answer to take time zone into account. There are at least two ways to get LocalDateTime for specific time zone. You can use setDefault time zone for whole application. It should be called before any timestamp -> java.time conversion: public static void main(String... args) { TimeZone utcTimeZone = TimeZone.getTimeZone("UTC"); TimeZone.setDefault(utcTimeZone); ... timestamp.toLocalDateTime().toLocalDate(); } Or you can use

Java 8 Convert given time and time zone to UTC time

北战南征 提交于 2019-11-28 16:51:33
I have a time with string type like: "2015-01-05 17:00" and ZoneId is "Australia/Sydney" . How can I convert this time information to the corresponding to UTC time using Java 8 datetime API? Also need to considering DST stuff. Mateusz Sroka You are looking for ZonedDateTime class in Java8 - a complete date-time with time-zone and resolved offset from UTC/Greenwich. In terms of design, this class should be viewed primarily as the combination of a LocalDateTime and a ZoneId . The ZoneOffset is a vital, but secondary, piece of information, used to ensure that the class represents an instant,

How to get UTC+0 date in Java 8?

烈酒焚心 提交于 2019-11-28 15:30:16
I have problems with Date class in Java. Date class returns local machine date but i need UTC-0. I have googled and found great solution for JavaScript but for Java nothing useful. How to get UTC+0 date in Java 8? With Java 8 you can write: OffsetDateTime utc = OffsetDateTime.now(ZoneOffset.UTC); To answer your comment, you can then convert it to a Date (unless you depend on legacy code I don't see any reason why) or to millis since the epochs: Date date = Date.from(utc.toInstant()); long epochMillis = utc.toEpochSecond() * 1000; tl;dr Instant.now() java.time The troublesome old date-time

Getting ZoneId from a SimpleTimeZone

寵の児 提交于 2019-11-28 14:46:51
Using Java I have a SimpleTimeZone instance with GMT offset and daylight saving time information from a legacy system. I would like to retrieve ZoneId to be able to use Java 8 time API. Actually, toZoneId returns a ZoneId without the daylight Saving time infos SimpleTimeZone stz = new SimpleTimeZone( 2 * 60 * 60 * 1000, "GMT", Calendar.JANUARY,1,1,1, Calendar.FEBRUARY,1,1,1, 1 * 60 * 60 * 1000); stz.toZoneId(); First of all, when you do: SimpleTimeZone stz = new SimpleTimeZone(2 * 60 * 60 * 1000, "GMT", Calendar.JANUARY, 1, 1, 1, Calendar.FEBRUARY, 1, 1, 1, 1 * 60 * 60 * 1000); You're creating

Writing and testing convenience methods using Java 8 Date/Time classes

混江龙づ霸主 提交于 2019-11-28 14:40:48
I have some old convenience methods, written using Calendars, that I want to update to use the Java.time.* classes introduced in Java 8. Some of the methods in my class get quantities like the current time or even just the current hour. I plan to write two variants of each method: one that assumes that the timezone is the default defined on this computer and one that allows the user to specify the desired timezone. I'm trying to figure out two main things: How to get the current timestamp in my methods. How to unit test my results using a different source of the current timestamp. With regards

Exception when trying to parse a LocalDateTime

↘锁芯ラ 提交于 2019-11-28 14:08:30
I am using the following timestamp format: yyyyMMddHHmmssSSS The following method works fine: public static String formatTimestamp(final Timestamp timestamp, final String format) { final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return timestamp.toLocalDateTime().format(formatter); } And, when I pass in a Timestamp with that format string, it returns, for example: 20170925142051591 I then require to map from that string to a Timestamp again, essentially the reverse operation. I know that I can use a SimpleDateFormat and its parse() method, but I'd prefer to stick to

How to handle full period in java.time?

為{幸葍}努か 提交于 2019-11-28 13:22:37
The Period class in java.time handles only the date-oriented potion: years, months, days. What about the time portion: hours, minutes, seconds? How can we parse and generate string representations of full periods as defined in ISO 8601 , PnYnMnDTnHnMnS ? For example, a day and a half: P1DT12H . The academic year is nine months, P9M . Every year I get two weeks and 3 days of vacation, P17D . The customer occupied the hotel room for 2 days and seventeen and a half hours, P2DT17H30M . The Period class in Joda-Time handles full period. Why not in java.time? Is there some other mechanism? In Java