java-time

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

Why does converting Java Dates before 1582 to LocalDate with Instant give a different date?

大城市里の小女人 提交于 2019-11-30 03:51:46
问题 Consider this code: Date date = new SimpleDateFormat("MMddyyyy").parse("01011500"); LocalDate localDateRight = LocalDate.parse(formatter.format(date), dateFormatter); LocalDate localDateWrong = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate(); System.out.println(date); // Wed Jan 01 00:00:00 EST 1500 System.out.println(localDateRight); // 1500-01-01 System.out.println(localDateWrong); // 1500-01-10 I know that 1582 is the cutoff between the Julian and Gregorian

Error java.time.format.DateTimeParseException: could not be parsed, unparsed text found at index 10

纵然是瞬间 提交于 2019-11-30 03:11:03
问题 I´m trying to pase the next String using LocalDateTime, but I always get de unparsed text found error: Error java.time.format.DateTimeParseException: Text '2016-08-18 14:27:15.103+02' could not be parsed, unparsed text found at index 10 Here is my String: convertDate: ' 2016-08-18 14:27:15.103+02 ' And my code: public static LocalDate conversorStringToLocalDateTime(String convertDate) throws ParseException { LocalDate dateTime =LocalDate.parse(convertDate); return dateTime; } I guess is not

Parse ISO timestamp using Java 8 java.time api (standard edition only)

為{幸葍}努か 提交于 2019-11-30 01:28:15
问题 I'm having trouble getting milliseconds from the epoch out of the string in the example. I have tried this three different ways so far, and the example shows the latest attempt. It always seems to come down to that the TemporalAccessor does not support ChronoField. If I could successfully construct an instance of Instant, I could use toEpochMilli(). String dateStr = "2014-08-16T05:03:45-05:00" TemporalAccessor creationAccessor = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(dateStr); Instant

How to skip weekends while adding days to LocalDate in Java 8?

自作多情 提交于 2019-11-29 23:34:50
Other answers here refer to Joda API. I want to do it using java.time . Suppose today's date is 26th Nov 2015-Thursday, when I add 2 business days to it, I want the result as Monday 30th Nov 2015. I am working on my own implementation but it would be great if something already exists! EDIT: Is there a way to do it apart from looping over? I was trying to derive a function like: Y = f(X1,X2) where Y is actual number of days to add, X1 is number of business days to add, X2 is day of the week (1-Monday to 7-Sunday) Then given X1 and X2 (derived from day of week of the date), we can find Y and

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

筅森魡賤 提交于 2019-11-29 23:11:44
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? Use Calendar: Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1); Date result = cal.getTime(); Starting from Java 8, the suggested way is to use the Date-Time API rather than Calendar . If you want a Date object to be returned: Date date = Date.from(ZonedDateTime.now().minusMonths(1).toInstant()); If you don't need exactly a

How can I have JAX-RS return a Java 8 LocalDateTime property as a JavaScript-style Date String?

放肆的年华 提交于 2019-11-29 18:33:22
问题 I created a RESTful web service using JAX-RS method annotations: @GET @Path("/test") @Produces(MediaType.APPLICATION_JSON) public MyThing test() { MyThing myObject = new MyThing(LocalDateTime.now()); return myObject; } This works nicely, but I'd like to adjust one thing: If the returned Java object contains a property of the new Java 8 LocalDateTime type, it is represented as a JSON object: {"myDateTimeProperty":{"hour":14,"minute":32,"second":39,"year":2014,"month":"NOVEMBER","dayOfMonth":6,

java date parse exception while conveting UTC to local time zone

不羁岁月 提交于 2019-11-29 17:57:29
I get a UTC date with following format and how can convert this UTC date to my local time zone? my input date is 2015-03-17 06:00:00 +0000 and I tried the following coding its gives parsing exception. My code: DateFormat utcFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss 'Z'"); utcFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = utcFormat.parse("2015-03-17 06:00:00 +0000"); // java.text.DateFormat.parse(Unknown Source) utcFormat.setTimeZone(TimeZone.getDefault()); System.out.println(utcFormat.format(date)); You have two mistakes in your code. As others already mentioned you have

Convert number of seconds into HH:MM (without seconds) in Java

血红的双手。 提交于 2019-11-29 17:39:48
I'm aware that you can use DateUtils.formatElapsedTime(seconds) to convert a number of seconds into a String with the format HH:MM:SS . But are there any utility functions that let me perform the same conversion but without the seconds? For example, I want to convert 3665 seconds into 1:01 , even though it's exactly 1:01:05 . In other words, simply dropping the seconds part. Would strongly prefer an answer that points to a utility function (if one exists) rather than a bunch of home rolled algorithms. Based on the available information, you seem to be wanting to format a duration based value.

Getting current time in Java 8

情到浓时终转凉″ 提交于 2019-11-29 17:03:53
问题 I am exploring the new java.time API of Java 8. I am particularly trying to retrieve the current time (my current time zone, of a different time zone, and of a different offset). The code is: public static void getCurrentLocalTime(){ LocalTime time = LocalTime.now(); System.out.println("Local Time Zone: "+ZoneId.systemDefault().toString()); System.out.println("Current local time : " + time); } public static void getCurrentTimeWithTimeZone(){ LocalDateTime localtDateAndTime = LocalDateTime.now