Java Date Time Java Java API
Class:
java.time.LocalTime
Methods:
public LocalTime with(TemporalAdjuster adjuster)
This method returns a new LocalTime instance, with adjusted fields per provided adjuster.
public LocalTime with(TemporalField field,
long newValue)
This method returns a new LocalTime instance, with adjusted fields per provided newValue and the target TemporalField
Examples
package com.logicbig.example.localtime;
import java.time.LocalTime; import java.time.temporal.*;
public class WithExample {
public static void main (String... args) { LocalTime t1 = LocalTime.of(1, 15, 30, 400); LocalTime t2 = t1.with(LocalTime.of(2, 40)); System.out.println(t2);
t2 = t1.with(lastMinOfHour()); System.out.println(t2); }
private static TemporalAdjuster lastMinOfHour () { return new TemporalAdjuster() { @Override public Temporal adjustInto (Temporal temporal) { LocalTime t = LocalTime.from(temporal);
return t.with(ChronoField.MINUTE_OF_HOUR, 59); } }; } }
Output02:40 01:59:30.000000400
This example creates a custom TemporalAdjuster. package com.logicbig.example.localtime;
import java.time.LocalTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAdjuster;
public class WithExample2 {
public static void main (String... args) { LocalTime t1 = LocalTime.of(1, 15, 30, 400); System.out.println(t1);
LocalTime t2 = t1.with(lastMinOfHour()); System.out.println(t2); }
private static TemporalAdjuster lastMinOfHour () { return temporal -> { LocalTime t = LocalTime.from(temporal);
return t.with(ChronoField.MINUTE_OF_HOUR, 59); }; } }
Output01:15:30.000000400 01:59:30.000000400
package com.logicbig.example.localtime;
import java.time.LocalTime; import java.time.temporal.ChronoField;
public class WithExample3 {
public static void main (String... args) { LocalTime d = LocalTime.of(2, 10, 20, 100); System.out.printf("Original local time: %s%n", d);
if (d.isSupported(ChronoField.NANO_OF_DAY)) { LocalTime d2 = d.with(ChronoField.NANO_OF_DAY, 5000); System.out.printf("5000th NANO_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.NANO_OF_SECOND)) { LocalTime d2 = d.with(ChronoField.NANO_OF_SECOND, 300); System.out.printf("300th NANO_OF_SECOND: %s%n", d2); }
if (d.isSupported(ChronoField.NANO_OF_DAY)) { LocalTime d2 = d.with(ChronoField.NANO_OF_DAY, 5000000); System.out.printf("5000000th NANO_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.MICRO_OF_SECOND)) { LocalTime d2 = d.with(ChronoField.MICRO_OF_SECOND, 1000); System.out.printf("1000th MICRO_OF_SECOND: %s%n", d2); }
if (d.isSupported(ChronoField.MICRO_OF_DAY)) { LocalTime d2 = d.with(ChronoField.MICRO_OF_DAY, 25000); System.out.printf("25000th NANO_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.MILLI_OF_SECOND)) { LocalTime d2 = d.with(ChronoField.MILLI_OF_SECOND, 300); System.out.printf("300th MILLI_OF_SECOND: %s%n", d2); }
if (d.isSupported(ChronoField.MILLI_OF_DAY)) { LocalTime d2 = d.with(ChronoField.MILLI_OF_DAY, 30000); System.out.printf("30000th MILLI_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.SECOND_OF_DAY)) { LocalTime d2 = d.with(ChronoField.SECOND_OF_DAY, 5); System.out.printf("5th SECOND_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.SECOND_OF_MINUTE)) { LocalTime d2 = d.with(ChronoField.SECOND_OF_MINUTE, 10); System.out.printf("10th SECOND_OF_MINUTE: %s%n", d2); }
if (d.isSupported(ChronoField.MINUTE_OF_DAY)) { LocalTime d2 = d.with(ChronoField.MINUTE_OF_DAY, 10); System.out.printf("10th MINUTE_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.MINUTE_OF_HOUR)) { LocalTime d2 = d.with(ChronoField.MINUTE_OF_HOUR, 20); System.out.printf("20th MINUTE_OF_HOUR: %s%n", d2); }
if (d.isSupported(ChronoField.MINUTE_OF_DAY)) { LocalTime d2 = d.with(ChronoField.MINUTE_OF_DAY, 120); System.out.printf("120 MINUTE_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.HOUR_OF_DAY)) { LocalTime d2 = d.with(ChronoField.HOUR_OF_DAY, 1); System.out.printf("Ist HOUR_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.HOUR_OF_AMPM)) { LocalTime d2 = d.with(ChronoField.HOUR_OF_AMPM, 2); System.out.printf("2nd HOUR_OF_AMPM: %s%n", d2); }
if (d.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM)) { LocalTime d2 = d.with(ChronoField.CLOCK_HOUR_OF_AMPM, 2); System.out.printf("2nd CLOCK_HOUR_OF_AMPM: %s%n", d2); }
if (d.isSupported(ChronoField.CLOCK_HOUR_OF_DAY)) { LocalTime d2 = d.with(ChronoField.CLOCK_HOUR_OF_DAY, 2); System.out.printf("2nd CLOCK_HOUR_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.AMPM_OF_DAY)) { LocalTime d2 = d.with(ChronoField.AMPM_OF_DAY, 1); System.out.printf("2nd CLOCK_HOUR_OF_DAY: %s%n", d2); }
if (d.isSupported(ChronoField.DAY_OF_MONTH)) { LocalTime d2 = d.with(ChronoField.DAY_OF_MONTH, 25); System.out.printf("25th DAY_OF_MONTH: %s%n", d2); }
if (d.isSupported(ChronoField.DAY_OF_WEEK)) { LocalTime d2 = d.with(ChronoField.DAY_OF_YEAR, 100); System.out.printf("100th DAY_OF_YEAR: %s%n", d2); }
if (d.isSupported(ChronoField.EPOCH_DAY)) { LocalTime d2 = d.with(ChronoField.EPOCH_DAY, 200); System.out.printf("200th EPOCH_DAY: %s%n", d2); } } }
OutputOriginal local time: 02:10:20.000000100 5000th NANO_OF_DAY: 00:00:00.000005 300th NANO_OF_SECOND: 02:10:20.000000300 5000000th NANO_OF_DAY: 00:00:00.005 1000th MICRO_OF_SECOND: 02:10:20.001 25000th NANO_OF_DAY: 00:00:00.025 300th MILLI_OF_SECOND: 02:10:20.300 30000th MILLI_OF_DAY: 00:00:30 5th SECOND_OF_DAY: 00:00:05.000000100 10th SECOND_OF_MINUTE: 02:10:10.000000100 10th MINUTE_OF_DAY: 00:10:20.000000100 20th MINUTE_OF_HOUR: 02:20:20.000000100 120 MINUTE_OF_DAY: 02:00:20.000000100 Ist HOUR_OF_DAY: 01:10:20.000000100 2nd HOUR_OF_AMPM: 02:10:20.000000100 2nd CLOCK_HOUR_OF_AMPM: 02:10:20.000000100 2nd CLOCK_HOUR_OF_DAY: 02:10:20.000000100 2nd CLOCK_HOUR_OF_DAY: 14:10:20.000000100
|
|