package com.logicbig.example.offsetdatetime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class WithSecondExample {
public static void main(String... args) {
OffsetDateTime d = OffsetDateTime.of(2015, 10, 5, 15,
30, 10, 1000000, ZoneOffset.ofHours(-5));
System.out.println(d);
OffsetDateTime d2 = d.withSecond(13);
System.out.println(d2);
}
}
Output
2015-10-05T15:30:10.001-05:00
2015-10-05T15:30:13.001-05:00
For invalid values of seconds
package com.logicbig.example.offsetdatetime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class WithSecondExample2 {
public static void main(String... args) {
OffsetDateTime d = OffsetDateTime.of(2015, 10, 5, 15,
30, 10, 1000000, ZoneOffset.ofHours(-5));
System.out.println(d);
OffsetDateTime d2 = d.withSecond(-10);
System.out.println(d2);
}
}
Output
Caused by: java.time.DateTimeException: Invalid value for SecondOfMinute (valid values 0 - 59): -10
at java.time.temporal.ValueRange.checkValidValue(ValueRange.java:311)
at java.time.temporal.ChronoField.checkValidValue(ChronoField.java:703)
at java.time.LocalTime.withSecond(LocalTime.java:907)
at java.time.LocalDateTime.withSecond(LocalDateTime.java:1081)
at java.time.OffsetDateTime.withSecond(OffsetDateTime.java:1091)
at com.logicbig.example.offsetdatetime.WithSecondExample2.main(WithSecondExample2.java:19)
... 6 more