java-date

How to find epoch format current time of GMT using java

与世无争的帅哥 提交于 2019-12-10 10:46:33
问题 I have written below code which is running, and giving output. But I'm not sure It's a right one. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); sdf.setTimeZone(TimeZone.getTimeZone("GMT-7")); String value = sdf.format(date); System.out.println(value); Date date2 = sdf.parse(value); long result = date2.getTime(); System.out.println(result); return result; The above code what I'm trying is, I just need to get the current time of GMT time zone and

Getting incorrect result when checking a date between two other dates in Java

谁说我不能喝 提交于 2019-12-08 07:55:16
问题 I'm using the code below to check if an hour is between two other specific hours: String openHour = "08:00 AM"; String currentHour = "10:00 PM"; String closeHour = "11:00 PM"; //Change to 02:00 AM doesn't work!!! SimpleDateFormat format = new SimpleDateFormat("hh:mm a"); Date openHourDate = format.parse(openHour); Date currentHourDate = format.parse(currentHour); Date closeHourDate = format.parse(closeHour); Calendar openCalendar = Calendar.getInstance(); openCalendar.setTime(openHourDate);

How do I update RestTemplate to correctly map Java Dates?

◇◆丶佛笑我妖孽 提交于 2019-12-02 07:43:55
问题 I have an issue where my RestTemplate.postForEntity(url, restRequest, RepoResponse.class) call is failing because it can't deserialise dates of the form: 2019-02-01T12:00:00.000-0500 because of the missing colon in the timezone. Based on this answer, it looks like I want to change the date formatting of my RestTemplate 's ObjectMapper . I've tried a solution here: https://stackoverflow.com/a/38286322/14250 Which gives me the following code: restTemplate = new RestTemplate(); ObjectMapper

How do I update RestTemplate to correctly map Java Dates?

橙三吉。 提交于 2019-12-02 04:12:07
I have an issue where my RestTemplate.postForEntity(url, restRequest, RepoResponse.class) call is failing because it can't deserialise dates of the form: 2019-02-01T12:00:00.000-0500 because of the missing colon in the timezone. Based on this answer , it looks like I want to change the date formatting of my RestTemplate 's ObjectMapper . I've tried a solution here: https://stackoverflow.com/a/38286322/14250 Which gives me the following code: restTemplate = new RestTemplate(); ObjectMapper objectMapper = new ObjectMapper(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss

Java Time: Get max number of weeks for particular year

五迷三道 提交于 2019-12-02 01:25:04
问题 I only found a solution for Joda Time. My solution works only if the last day is not in the first week: LocalDate.now() // or any other LocalDate .withDayOfMonth(31) .withMonth(12) .get(weekFields.weekOfWeekBasedYear()) So what is the correct way in Java Time (like in Joda Time)? 回答1: This information is available directly using the java.time.* API. The key method is rangeRefinedBy(Temporal) on TemporalField . It allows you to obtain a ValueRange object that provides the minimum and maximum

Java 8 Date API - Get total number of weeks in a month

折月煮酒 提交于 2019-12-01 19:46:12
I have a Kotlin function to get the total number of weeks in a month Code fun getTotalWeeksInMonth(instant: Instant): Int { val calendar = Calendar.getInstance() calendar.time = Date.from(instant) return calendar.getActualMaximum(Calendar.WEEK_OF_MONTH) } However this is using a mix of the old Java date/time APIs ( Date and Calendar ) and the new APIs ( Instant ) How would I achieve the same result, using just the new APIs? Schidu Luca You can try something like this pair of lines: YearMonth currentYearMonth = YearMonth.now( ZoneId.systemDefault() ) ; int weeks = currentYearMonth .atEndOfMonth

LocalDateTime , ZonedDateTime and Timestamp

痞子三分冷 提交于 2019-11-30 04:05:21
I have a SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I have a domain object with 2 properties (initDate, endDate). I want to create 2 converters to deal with mySQL DB @Convert(converter = LocalDateTimeAttributeConverter.class) private LocalDateTime initDate; @Convert(converter = ZonedDateTimeAttributeConverter.class) private ZonedDateTime endDate; the converter 1 (is OK) @Converter public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> { @Override public Timestamp

JPA Storing OffsetDateTime with ZoneOffset

*爱你&永不变心* 提交于 2019-11-29 06:16:29
问题 I have the following entity class: @Entity public class Event { private OffsetDateTime startDateTime; // ... } However, persisting and then reading the entity to/from the database with JPA 2.2 results in a loss of information : the ZoneOffset of startDateTime changes to UTC (the ZoneOffset used by the database timestamp). For instance: Event e = new Event(); e.setStartDateTime(OffsetDateTime.parse("2018-01-02T09:00-05:00")); e.getStartDateTime().getHour(); // 9 e.getStartDateTime().getOffset(

LocalDateTime , ZonedDateTime and Timestamp

匆匆过客 提交于 2019-11-29 01:19:14
问题 I have a SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I have a domain object with 2 properties (initDate, endDate). I want to create 2 converters to deal with mySQL DB @Convert(converter = LocalDateTimeAttributeConverter.class) private LocalDateTime initDate; @Convert(converter = ZonedDateTimeAttributeConverter.class) private ZonedDateTime endDate; the converter 1 (is OK) @Converter public class

How to parse a UTC offset dateformat string into the resulting date separated by | symbol

五迷三道 提交于 2019-11-28 02:05:53
I have a very peculiar question where I am trying to parse "2019-12-25T17:00:00-05:00" such that it should give me the result DEC 12 | Thursday | 5:00pm I tried the following code by using DateTimeFormatter and LocalDate DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz", Locale.US); DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM d | E | hh:mm a", Locale.US); LocalDate date = LocalDate.parse("2019-12-25T17:00:00-05:00", inputFormatter); String formattedDate = outputFormatter.format(date); contentTextView.setText(formattedDate); but it