java-time

How to add/subtract TimeZone Offset to a Timestamp in Java?

对着背影说爱祢 提交于 2019-12-10 18:53:33
问题 I am using JDK 8 and I played with ZonedDateTime and Timestamp a lot. But still I am not able to get a solution for the problem I am facing. Lets say I get a formatted Timestamp in GMT (UTC) and my server is located somewhere. Lets say it's set to Asia/Calcutta TimeZone (whose ZoneOffset is +05:30 ). How to basically add/subtract my time zone offset to the Timestamp? Input : 2017-09-13 13:10:30.333 Output: 2017-09-13 18:40:30.333 Explanation : In the input, the timezone offset which is 5hrs

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDate; error

无人久伴 提交于 2019-12-10 18:24:14
问题 I tried everything, but the error not getting solved Here is my code: public String[] getWeekDays() { LocalDate today = LocalDate.now(); LocalDate monday = today; LocalDate tue = today; LocalDate wed = today; LocalDate thur = today; LocalDate fri = today; LocalDate sat = today; LocalDate sunday = today; while (sunday.getDayOfWeek() != DayOfWeek.SUNDAY){ sunday = sunday.minusDays(1); } while (monday.getDayOfWeek() != DayOfWeek.MONDAY){ monday = monday.minusDays(1); } while (tue.getDayOfWeek()

Interrogate a ZonedDateTime asking if in standard time or in Daylight Saving Time (DST) [duplicate]

☆樱花仙子☆ 提交于 2019-12-10 18:11:46
问题 This question already has an answer here : Java8 - how to know if daylight savings is on now (1 answer) Closed 3 years ago . How to ask a java.time.ZonedDateTime object if Daylight Saving Time (DST) applies to its moment or if standard time applies? 回答1: @Jon's answer is good. Just want to mention there is ZoneRules#isDaylightSavings available. ZonedDateTime zdt = ...; ZoneRules rules = zdt.getZone().getRules(); boolean isDst = rules.isDaylightSavings(zdt.toInstant()); And possible duplicate

how to convert UTC time to some other time zone (“CST”,“IST”)

喜欢而已 提交于 2019-12-10 17:33:59
问题 In my Android application server will return some UTC date in following format( yyyy-MM-dd HH:mm:ss ) 24hours and I need to convert those time into user's TimeZone for example CST , IST . I did the following code but I do know is it correct or not, please assist me to do the time zone conversion in right way. I get UTC date as json string and converting into user's time zone format and showing Android side private static Date utcDate; private static DateFormat expireFormat = new

Inconsistent `w` symbol formatting from joda time to java.time

梦想的初衷 提交于 2019-12-10 17:29:02
问题 My team is looking to switch from Joda time to java.time , but we're seeing different behavior in formatting using the same pattern. The issue arises when we're using the week-of-week-year w symbol: final String dateString = "2016-01-04 00:00:00"; final String inputPattern = "yyyy-MM-dd HH:mm:ss"; // parse the input string using Joda final org.joda.time.format.DateTimeFormatter jodaInputFormatter = org.joda.time.format.DateTimeFormat.forPattern(inputPattern); final org.joda.time.DateTime

Jooq binding for “timestamp with time zone” type in postgres

允我心安 提交于 2019-12-10 15:34:25
问题 Jooq currently does not support JSR 310 types and support will not come until v3.8. Using simple converters generally works, except for certain types, such as postgres' TIMESTAMP WITH TIME ZONE , which requires a custom binding. So I have tried to write one but the generated XxxRecord classes still use a Timestamp data type for the TIMESTAMP WITH TIME ZONE fields in my DB. What do I need to change in my code below to see postgres' TIMESTAMP WITH TIME ZONE as an Instant in jooq's generated

java.time DateTimeFormatter parsing with flexible fallback values

会有一股神秘感。 提交于 2019-12-10 13:43:54
问题 I am trying to port some code from joda time to java time. JodaTime had the possibility to specify a fallback value for the year like this parser.withDefaultYear((new DateTime(DateTimeZone.UTC)).getYear()).parseDateTime(text); Regardless how the parser looks (if it includes a year or not), this will be parsed. java.time has become much more stricter there. Even though there is the DateTimeFormatterBuilder.parseDefaulting() method, which allows you to specify fallbacks, this only works if that

Convert java.sql.Timestamp to Java 8 ZonedDateTime?

烈酒焚心 提交于 2019-12-10 13:12:26
问题 Migrating Joda time to Java 8 Joda: UserObject user = new UserObject() user.setCreatedAt(new DateTime(rs.getTimestamp("columnName")));` Migrating to Java 8 This is my code; it does compile; I am doubtful if it works: ZonedDateTime.ofInstant(rs.getTimestamp("columnName").toLocalDateTime().toInstant(ZoneOffset.UTC),ZoneId.of("UTC"))); In some cases, the date is wrong. Any advice? 回答1: tl;dr To track a moment in history, use Instant as the type of your class member variable. Specifically, this

Java 8 timezone conversion

丶灬走出姿态 提交于 2019-12-10 11:13:38
问题 I know there are similar questions like this, but I wasn't quite able to find the example similar to mine. I learned about LocalDateTime and ZonedDateTime but I don't know how to tell my ZonedDateTime what's assumed timezone of parsed date. I'm migrating from Java 7 to Java 8. In my code, I have a method like this: public String changeTZ(String tzFrom, String tzTo, String dateToChange) { ZoneId zoneFrom = ZoneId.of(tzFrom); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat

How to create a Bank Calendar with 30 days each month

天大地大妈咪最大 提交于 2019-12-10 10:05:53
问题 For calculating annuity loan with german I need a calendar where every month has exactly 30 days. Every year has 360 days. There are no leap years. The interest is allways calculated on a basis of 30 day (That's the german interest method) I'm using Java 8. What possibilities do I have with the new java.time API to accomplish my requirements? 回答1: What you describe is a day count not a calendar system. A day count is a mechanism for converting the period between two dates into a year fraction