Close

Java Date Time - LocalDateTime.of() Examples

Java Date Time Java Java API 


Class:

java.time.LocalDateTime

java.lang.Objectjava.lang.Objectjava.time.LocalDateTimejava.time.LocalDateTimejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.time.chrono.ChronoLocalDateTimeChronoLocalDateTimejava.io.SerializableSerializableLogicBig

Methods:

public static LocalDateTime of(int year,

Month month,

int dayOfMonth,

int hour,

int minute)

public static LocalDateTime of(int year,

Month month,

int dayOfMonth,

int hour,

int minute,

int second)

public static LocalDateTime of(int year,

Month month,

int dayOfMonth,

int hour,

int minute,

int second,

int nanoOfSecond)

public static LocalDateTime of(int year,

int month,

int dayOfMonth,

int hour,

int minute)

public static LocalDateTime of(int year,

int month,

int dayOfMonth,

int hour,

int minute,

int second)

public static LocalDateTime of(int year,

int month,

int dayOfMonth,

int hour,

int minute,

int second,

int nanoOfSecond)

public static LocalDateTime of(LocalDate date,

LocalTime time)

These methods return a new LocalDateTime instance from the specified year, month, dayOfMonth, minutes and seconds. The overloaded methods also have optional parameters: second and nanoOfSecond.

If provided values are not valid DateTimeException is thrown.



Examples


package com.logicbig.example.localdatetime;

import java.time.LocalDateTime;
import java.time.Month;

public class OfExample {

public static void main (String... args) {
LocalDateTime d = LocalDateTime.of(2016, Month.JANUARY, 1,
22, 20);
System.out.println(d);
}

}

Output

2016-01-01T22:20




package com.logicbig.example.localdatetime;

import java.time.LocalDateTime;
import java.time.Month;

public class OfExample2 {


public static void main (String... args) {
LocalDateTime d = LocalDateTime.of(2016, Month.JANUARY, 1,
22, 20, 30);
System.out.println(d);
}

}

Output

2016-01-01T22:20:30




package com.logicbig.example.localdatetime;

import java.time.LocalDateTime;
import java.time.Month;

public class OfExample3 {

public static void main (String... args) {
LocalDateTime d = LocalDateTime.of(2016, Month.JANUARY, 1,
22, 20, 30, 200000);
System.out.println(d);
}
}

Output

2016-01-01T22:20:30.000200




package com.logicbig.example.localdatetime;

import java.time.LocalDateTime;

public class OfExample4 {

public static void main (String... args) {
LocalDateTime d = LocalDateTime.of(2016, 1, 1,
22, 20);
System.out.println(d);
}

}

Output

2016-01-01T22:20

package com.logicbig.example.localdatetime;

import java.time.LocalDateTime;

public class OfExample5 {

public static void main (String... args) {
LocalDateTime d = LocalDateTime.of(2016, 1, 1,
22, 20, 30);
System.out.println(d);
}
}

Output

2016-01-01T22:20:30




package com.logicbig.example.localdatetime;

import java.time.LocalDateTime;

public class OfExample6 {

public static void main (String... args) {
LocalDateTime d = LocalDateTime.of(2016, 1, 1,
22, 20, 30, 20000000);
System.out.println(d);

}
}

Output

2016-01-01T22:20:30.020




package com.logicbig.example.localdatetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class OfExample7 {

public static void main (String... args) {
LocalDate d = LocalDate.of(2016, 1, 1);
LocalTime t = LocalTime.of(10, 20, 30, 2000000);

LocalDateTime dt = LocalDateTime.of(d, t);
System.out.println(dt);

}
}

Output

2016-01-01T10:20:30.002




See Also