Java Date

In Java Programming there are available many package. This package are providing classes those are given functionality to implement date and time. In this section are given examples to use date and time API.

java.util.Date

import java.util.Date; //for LocalDate
public class MyDate{
    public static void main(String[] args){
      Date date=new Date();  
      System.out.println(date);  
    }
}
Mon Jul 22 09:47:01 IST 2019
Example INSTANCE of java.util.Date

Private instance variable of date objects are accessible within the private and public methods of Date class. In below table are mention all public methods which is accessible by Date objects.

Methods Syntax
UTC public static long java.util.Date.UTC(int,int,int,int,int,int)
getYear public int java.util.Date.getYear()
setYear public void java.util.Date.setYear(int)
getMonth public int java.util.Date.getMonth()
setMonth public void java.util.Date.setMonth(int)
setDate public void java.util.Date.setDate(int)
getDay public int java.util.Date.getDay()
getHours public int java.util.Date.getHours()
setHours public void java.util.Date.setHours(int)
getMinutes public int java.util.Date.getMinutes()
setMinutes public void java.util.Date.setMinutes(int)
getSeconds public int java.util.Date.getSeconds()
setSeconds public void java.util.Date.setSeconds(int)
getTime public long java.util.Date.getTime()
setTime public void java.util.Date.setTime(long)
toLocaleString public java.lang.String java.util.Date.toLocaleString()
toGMTString public java.lang.String java.util.Date.toGMTString()
getTimezoneOffset public int java.util.Date.getTimezoneOffset()
from public static java.util.Date java.util.Date.from(java.time.Instant)
toInstant public java.time.Instant java.util.Date.toInstant()
getDate public int java.util.Date.getDate()
equals public boolean java.util.Date.equals(java.lang.Object)
toString public java.lang.String java.util.Date.toString()
hashCode public int java.util.Date.hashCode()
clone public java.lang.Object java.util.Date.clone()
compareTo public int java.util.Date.compareTo(java.util.Date)
compareTo public int java.util.Date.compareTo(java.lang.Object)
parse public static long java.util.Date.parse(java.lang.String)
before public boolean java.util.Date.before(java.util.Date)
after public boolean java.util.Date.after(java.util.Date)
wait public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait public final void java.lang.Object.wait() throws java.lang.InterruptedException
getClass public final native java.lang.Class java.lang.Object.getClass()
notify public final native void java.lang.Object.notify()
notifyAll public final native void java.lang.Object.notifyAll()

java.time.LocalDate

Java java.time package are provide various class to deal with date and time. Let see an example to display simple date.

//Display Current date
import java.time.LocalDate; //for LocalDate
public class MyDate{
    public static void main(String[] args){
      //Create objects of LocalDate
      LocalDate date = LocalDate.now();

      System.out.println("Data : "+date);
    }
}
LocalDate INSTANCE Example
Data : 2019-07-21

That is very simple example the result are displayed in format of Year-Month-Day. There are following public methods are accessible within LocalDate class.

Methods Syntax
ofYearDay public static java.time.LocalDate java.time.LocalDate.ofYearDay(int,int)
ofEpochDay public static java.time.LocalDate java.time.LocalDate.ofEpochDay(long)
from public static java.time.LocalDate java.time.LocalDate.from(java.time.temporal.TemporalAccessor)
getChronology public java.time.chrono.Chronology java.time.LocalDate.getChronology()
getChronology public java.time.chrono.IsoChronology java.time.LocalDate.getChronology()
getEra public java.time.chrono.Era java.time.LocalDate.getEra()
getYear public int java.time.LocalDate.getYear()
getMonthValue public int java.time.LocalDate.getMonthValue()
getMonth public java.time.Month java.time.LocalDate.getMonth()
getDayOfMonth public int java.time.LocalDate.getDayOfMonth()
getDayOfYear public int java.time.LocalDate.getDayOfYear()
getDayOfWeek public java.time.DayOfWeek java.time.LocalDate.getDayOfWeek()
isLeapYear public boolean java.time.LocalDate.isLeapYear()
lengthOfMonth public int java.time.LocalDate.lengthOfMonth()
lengthOfYear public int java.time.LocalDate.lengthOfYear()
with public java.time.chrono.ChronoLocalDate java.time.LocalDate.with(java.time.temporal.TemporalField,long)
with public java.time.temporal.Temporal java.time.LocalDate.with(java.time.temporal.TemporalAdjuster)
with public java.time.chrono.ChronoLocalDate java.time.LocalDate.with(java.time.temporal.TemporalAdjuster)
with public java.time.LocalDate java.time.LocalDate.with(java.time.temporal.TemporalAdjuster)
with public java.time.LocalDate java.time.LocalDate.with(java.time.temporal.TemporalField,long)
with public java.time.temporal.Temporal java.time.LocalDate.with(java.time.temporal.TemporalField,long)
withYear public java.time.LocalDate java.time.LocalDate.withYear(int)
withMonth public java.time.LocalDate java.time.LocalDate.withMonth(int)
withDayOfMonth public java.time.LocalDate java.time.LocalDate.withDayOfMonth(int)
withDayOfYear public java.time.LocalDate java.time.LocalDate.withDayOfYear(int)
plus public java.time.temporal.Temporal java.time.LocalDate.plus(java.time.temporal.TemporalAmount)
plus public java.time.temporal.Temporal java.time.LocalDate.plus(long,java.time.temporal.TemporalUnit)
plus public java.time.chrono.ChronoLocalDate java.time.LocalDate.plus(java.time.temporal.TemporalAmount)
plus public java.time.LocalDate java.time.LocalDate.plus(java.time.temporal.TemporalAmount)
plus public java.time.LocalDate java.time.LocalDate.plus(long,java.time.temporal.TemporalUnit)
plus public java.time.chrono.ChronoLocalDate java.time.LocalDate.plus(long,java.time.temporal.TemporalUnit)
plusYears public java.time.LocalDate java.time.LocalDate.plusYears(long)
plusMonths public java.time.LocalDate java.time.LocalDate.plusMonths(long)
plusWeeks public java.time.LocalDate java.time.LocalDate.plusWeeks(long)
plusDays public java.time.LocalDate java.time.LocalDate.plusDays(long)
minus public java.time.LocalDate java.time.LocalDate.minus(java.time.temporal.TemporalAmount)
minus public java.time.LocalDate java.time.LocalDate.minus(long,java.time.temporal.TemporalUnit)
minus public java.time.chrono.ChronoLocalDate java.time.LocalDate.minus(java.time.temporal.TemporalAmount)
minus public java.time.chrono.ChronoLocalDate java.time.LocalDate.minus(long,java.time.temporal.TemporalUnit)
minus public java.time.temporal.Temporal java.time.LocalDate.minus(java.time.temporal.TemporalAmount)
minus public java.time.temporal.Temporal java.time.LocalDate.minus(long,java.time.temporal.TemporalUnit)
minusYears public java.time.LocalDate java.time.LocalDate.minusYears(long)
minusMonths public java.time.LocalDate java.time.LocalDate.minusMonths(long)
minusWeeks public java.time.LocalDate java.time.LocalDate.minusWeeks(long)
minusDays public java.time.LocalDate java.time.LocalDate.minusDays(long)
adjustInto public java.time.temporal.Temporal java.time.LocalDate.adjustInto(java.time.temporal.Temporal)
until public long java.time.LocalDate.until(java.time.temporal.Temporal,java.time.temporal.TemporalUnit)
until public java.time.Period java.time.LocalDate.until(java.time.chrono.ChronoLocalDate)
until public java.time.chrono.ChronoPeriod java.time.LocalDate.until(java.time.chrono.ChronoLocalDate)
atTime public java.time.LocalDateTime java.time.LocalDate.atTime(int,int)
atTime public java.time.LocalDateTime java.time.LocalDate.atTime(int,int,int)
atTime public java.time.LocalDateTime java.time.LocalDate.atTime(int,int,int,int)
atTime public java.time.OffsetDateTime java.time.LocalDate.atTime(java.time.OffsetTime)
atTime public java.time.chrono.ChronoLocalDateTime java.time.LocalDate.atTime(java.time.LocalTime)
atTime public java.time.LocalDateTime java.time.LocalDate.atTime(java.time.LocalTime)
atStartOfDay public java.time.ZonedDateTime java.time.LocalDate.atStartOfDay(java.time.ZoneId)
atStartOfDay public java.time.LocalDateTime java.time.LocalDate.atStartOfDay()
toEpochDay public long java.time.LocalDate.toEpochDay()
isAfter public boolean java.time.LocalDate.isAfter(java.time.chrono.ChronoLocalDate)
isBefore public boolean java.time.LocalDate.isBefore(java.time.chrono.ChronoLocalDate)
isEqual public boolean java.time.LocalDate.isEqual(java.time.chrono.ChronoLocalDate)
now public static java.time.LocalDate java.time.LocalDate.now(java.time.Clock)
now public static java.time.LocalDate java.time.LocalDate.now(java.time.ZoneId)
now public static java.time.LocalDate java.time.LocalDate.now()
get public int java.time.LocalDate.get(java.time.temporal.TemporalField)
equals public boolean java.time.LocalDate.equals(java.lang.Object)
toString public java.lang.String java.time.LocalDate.toString()
hashCode public int java.time.LocalDate.hashCode()
compareTo public int java.time.LocalDate.compareTo(java.lang.Object)
compareTo public int java.time.LocalDate.compareTo(java.time.chrono.ChronoLocalDate)
getLong public long java.time.LocalDate.getLong(java.time.temporal.TemporalField)
format public java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)
query public java.lang.Object java.time.LocalDate.query(java.time.temporal.TemporalQuery)
of public static java.time.LocalDate java.time.LocalDate.of(int,int,int)
of public static java.time.LocalDate java.time.LocalDate.of(int,java.time.Month,int)
isSupported public boolean java.time.LocalDate.isSupported(java.time.temporal.TemporalField)
isSupported public boolean java.time.LocalDate.isSupported(java.time.temporal.TemporalUnit)
parse public static java.time.LocalDate java.time.LocalDate.parse(java.lang.CharSequence)
parse public static java.time.LocalDate java.time.LocalDate.parse(java.lang.CharSequence,java.time.format.DateTimeFormatter)
range public java.time.temporal.ValueRange java.time.LocalDate.range(java.time.temporal.TemporalField)
wait public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait public final void java.lang.Object.wait() throws java.lang.InterruptedException
getClass public final native java.lang.Class java.lang.Object.getClass()
notify public final native void java.lang.Object.notify()
notifyAll public final native void java.lang.Object.notifyAll()

For example

//Display Current date
import java.time.LocalDate; //for LocalDate
public class MyDate{
    public static void main(String[] args){
      //Create objects of LocalDate
      LocalDate date = LocalDate.now();
      System.out.println("Day : "+date.getDayOfMonth());
      System.out.println("Month : "+date.getMonth());
      System.out.println("Year : "+date.getYear());
    }
}
Day : 4
Month : JULY
Year : 2019

java.time.LocalDateTime

In case, you'll are need to find exact date and time then you can use java.time.LocalDateTime package.

import java.time.LocalDateTime; //for LocalDateTime
public class MyDate{
    public static void main(String[] args){
      //Create objects of LocalDateTime
      LocalDateTime dateTime = LocalDateTime.now();

      System.out.println("Data and Time : "+dateTime);
    }
}
LocalDateTime INSTANCE Example
Data and Time : 2019-07-04T04:48:44.736

There are following public methods are accessible within java.time.LocalDateTime.

Methods Syntax
ofInstant public static java.time.LocalDateTime java.time.LocalDateTime.ofInstant(java.time.Instant,java.time.ZoneId)
ofEpochSecond public static java.time.LocalDateTime java.time.LocalDateTime.ofEpochSecond(long,int,java.time.ZoneOffset)
from public static java.time.LocalDateTime java.time.LocalDateTime.from(java.time.temporal.TemporalAccessor)
with public java.time.LocalDateTime java.time.LocalDateTime.with(java.time.temporal.TemporalField,long)
with public java.time.LocalDateTime java.time.LocalDateTime.with(java.time.temporal.TemporalAdjuster)
with public java.time.temporal.Temporal java.time.LocalDateTime.with(java.time.temporal.TemporalAdjuster)
with public java.time.temporal.Temporal java.time.LocalDateTime.with(java.time.temporal.TemporalField,long)
with public java.time.chrono.ChronoLocalDateTime java.time.LocalDateTime.with(java.time.temporal.TemporalField,long)
with public java.time.chrono.ChronoLocalDateTime java.time.LocalDateTime.with(java.time.temporal.TemporalAdjuster)
toLocalDate public java.time.chrono.ChronoLocalDate java.time.LocalDateTime.toLocalDate()
toLocalDate public java.time.LocalDate java.time.LocalDateTime.toLocalDate()
getYear public int java.time.LocalDateTime.getYear()
getMonthValue public int java.time.LocalDateTime.getMonthValue()
getMonth public java.time.Month java.time.LocalDateTime.getMonth()
getDayOfMonth public int java.time.LocalDateTime.getDayOfMonth()
getDayOfYear public int java.time.LocalDateTime.getDayOfYear()
getDayOfWeek public java.time.DayOfWeek java.time.LocalDateTime.getDayOfWeek()
toLocalTime public java.time.LocalTime java.time.LocalDateTime.toLocalTime()
getHour public int java.time.LocalDateTime.getHour()
getMinute public int java.time.LocalDateTime.getMinute()
getSecond public int java.time.LocalDateTime.getSecond()
getNano public int java.time.LocalDateTime.getNano()
withYear public java.time.LocalDateTime java.time.LocalDateTime.withYear(int)
withMonth public java.time.LocalDateTime java.time.LocalDateTime.withMonth(int)
withDayOfMonth public java.time.LocalDateTime java.time.LocalDateTime.withDayOfMonth(int)
withDayOfYear public java.time.LocalDateTime java.time.LocalDateTime.withDayOfYear(int)
withHour public java.time.LocalDateTime java.time.LocalDateTime.withHour(int)
withMinute public java.time.LocalDateTime java.time.LocalDateTime.withMinute(int)
withSecond public java.time.LocalDateTime java.time.LocalDateTime.withSecond(int)
withNano public java.time.LocalDateTime java.time.LocalDateTime.withNano(int)
truncatedTo public java.time.LocalDateTime java.time.LocalDateTime.truncatedTo(java.time.temporal.TemporalUnit)
plus public java.time.LocalDateTime java.time.LocalDateTime.plus(long,java.time.temporal.TemporalUnit)
plus public java.time.chrono.ChronoLocalDateTime java.time.LocalDateTime.plus(long,java.time.temporal.TemporalUnit)
plus public java.time.chrono.ChronoLocalDateTime java.time.LocalDateTime.plus(java.time.temporal.TemporalAmount)
plus public java.time.temporal.Temporal java.time.LocalDateTime.plus(long,java.time.temporal.TemporalUnit)
plus public java.time.LocalDateTime java.time.LocalDateTime.plus(java.time.temporal.TemporalAmount)
plus public java.time.temporal.Temporal java.time.LocalDateTime.plus(java.time.temporal.TemporalAmount)
plusYears public java.time.LocalDateTime java.time.LocalDateTime.plusYears(long)
plusMonths public java.time.LocalDateTime java.time.LocalDateTime.plusMonths(long)
plusWeeks public java.time.LocalDateTime java.time.LocalDateTime.plusWeeks(long)
plusDays public java.time.LocalDateTime java.time.LocalDateTime.plusDays(long)
plusHours public java.time.LocalDateTime java.time.LocalDateTime.plusHours(long)
plusMinutes public java.time.LocalDateTime java.time.LocalDateTime.plusMinutes(long)
plusSeconds public java.time.LocalDateTime java.time.LocalDateTime.plusSeconds(long)
plusNanos public java.time.LocalDateTime java.time.LocalDateTime.plusNanos(long)
minus public java.time.chrono.ChronoLocalDateTime java.time.LocalDateTime.minus(java.time.temporal.TemporalAmount)
minus public java.time.LocalDateTime java.time.LocalDateTime.minus(long,java.time.temporal.TemporalUnit)
minus public java.time.LocalDateTime java.time.LocalDateTime.minus(java.time.temporal.TemporalAmount)
minus public java.time.temporal.Temporal java.time.LocalDateTime.minus(long,java.time.temporal.TemporalUnit)
minus public java.time.temporal.Temporal java.time.LocalDateTime.minus(java.time.temporal.TemporalAmount)
minus public java.time.chrono.ChronoLocalDateTime java.time.LocalDateTime.minus(long,java.time.temporal.TemporalUnit)
minusYears public java.time.LocalDateTime java.time.LocalDateTime.minusYears(long)
minusMonths public java.time.LocalDateTime java.time.LocalDateTime.minusMonths(long)
minusWeeks public java.time.LocalDateTime java.time.LocalDateTime.minusWeeks(long)
minusDays public java.time.LocalDateTime java.time.LocalDateTime.minusDays(long)
minusHours public java.time.LocalDateTime java.time.LocalDateTime.minusHours(long)
minusMinutes public java.time.LocalDateTime java.time.LocalDateTime.minusMinutes(long)
minusSeconds public java.time.LocalDateTime java.time.LocalDateTime.minusSeconds(long)
minusNanos public java.time.LocalDateTime java.time.LocalDateTime.minusNanos(long)
adjustInto public java.time.temporal.Temporal java.time.LocalDateTime.adjustInto(java.time.temporal.Temporal)
until public long java.time.LocalDateTime.until(java.time.temporal.Temporal,java.time.temporal.TemporalUnit)
atOffset public java.time.OffsetDateTime java.time.LocalDateTime.atOffset(java.time.ZoneOffset)
atZone public java.time.ZonedDateTime java.time.LocalDateTime.atZone(java.time.ZoneId)
atZone public java.time.chrono.ChronoZonedDateTime java.time.LocalDateTime.atZone(java.time.ZoneId)
isAfter public boolean java.time.LocalDateTime.isAfter(java.time.chrono.ChronoLocalDateTime)
isBefore public boolean java.time.LocalDateTime.isBefore(java.time.chrono.ChronoLocalDateTime)
isEqual public boolean java.time.LocalDateTime.isEqual(java.time.chrono.ChronoLocalDateTime)
now public static java.time.LocalDateTime java.time.LocalDateTime.now(java.time.Clock)
now public static java.time.LocalDateTime java.time.LocalDateTime.now(java.time.ZoneId)
now public static java.time.LocalDateTime java.time.LocalDateTime.now()
get public int java.time.LocalDateTime.get(java.time.temporal.TemporalField)
equals public boolean java.time.LocalDateTime.equals(java.lang.Object)
toString public java.lang.String java.time.LocalDateTime.toString()
hashCode public int java.time.LocalDateTime.hashCode()
compareTo public int java.time.LocalDateTime.compareTo(java.lang.Object)
compareTo public int java.time.LocalDateTime.compareTo(java.time.chrono.ChronoLocalDateTime)
getLong public long java.time.LocalDateTime.getLong(java.time.temporal.TemporalField)
format public java.lang.String java.time.LocalDateTime.format(java.time.format.DateTimeFormatter)
query public java.lang.Object java.time.LocalDateTime.query(java.time.temporal.TemporalQuery)
of public static java.time.LocalDateTime java.time.LocalDateTime.of(int,java.time.Month,int,int,int,int)
of public static java.time.LocalDateTime java.time.LocalDateTime.of(int,int,int,int,int)
of public static java.time.LocalDateTime java.time.LocalDateTime.of(int,java.time.Month,int,int,int,int,int)
of public static java.time.LocalDateTime java.time.LocalDateTime.of(int,int,int,int,int,int)
of public static java.time.LocalDateTime java.time.LocalDateTime.of(int,java.time.Month,int,int,int)
of public static java.time.LocalDateTime java.time.LocalDateTime.of(int,int,int,int,int,int,int)
of public static java.time.LocalDateTime java.time.LocalDateTime.of(java.time.LocalDate,java.time.LocalTime)
isSupported public boolean java.time.LocalDateTime.isSupported(java.time.temporal.TemporalField)
isSupported public boolean java.time.LocalDateTime.isSupported(java.time.temporal.TemporalUnit)
parse public static java.time.LocalDateTime java.time.LocalDateTime.parse(java.lang.CharSequence)
parse public static java.time.LocalDateTime java.time.LocalDateTime.parse(java.lang.CharSequence,java.time.format.DateTimeFormatter)
range public java.time.temporal.ValueRange java.time.LocalDateTime.range(java.time.temporal.TemporalField)
wait public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait public final void java.lang.Object.wait() throws java.lang.InterruptedException
getClass public final native java.lang.Class java.lang.Object.getClass()
notify public final native void java.lang.Object.notify()
notifyAll public final native void java.lang.Object.notifyAll()
getChronology public default java.time.chrono.Chronology java.time.chrono.ChronoLocalDateTime.getChronology()
toInstant public default java.time.Instant java.time.chrono.ChronoLocalDateTime.toInstant(java.time.ZoneOffset)
toEpochSecond public default long java.time.chrono.ChronoLocalDateTime.toEpochSecond(java.time.ZoneOffset)

java.time.LocalTime

If In case you'll are only need to get the current time. Then you can use java.time.LocalTime package.

import java.time.LocalTime;; //for LocalTime
public class MyDate{
    public static void main(String[] args){
      //Create objects of LocalTime
      LocalTime time = LocalTime.now();

      System.out.println("Time : "+time);
    }
}
LocalTime INSTANCE Example
Time : 16:34:55.096

Method which is publicly accessible in java.time.LocalTime package.

Methods Syntax
ofSecondOfDay public static java.time.LocalTime java.time.LocalTime.ofSecondOfDay(long)
ofNanoOfDay public static java.time.LocalTime java.time.LocalTime.ofNanoOfDay(long)
from public static java.time.LocalTime java.time.LocalTime.from(java.time.temporal.TemporalAccessor)
getHour public int java.time.LocalTime.getHour()
getMinute public int java.time.LocalTime.getMinute()
getSecond public int java.time.LocalTime.getSecond()
getNano public int java.time.LocalTime.getNano()
with public java.time.temporal.Temporal java.time.LocalTime.with(java.time.temporal.TemporalField,long)
with public java.time.temporal.Temporal java.time.LocalTime.with(java.time.temporal.TemporalAdjuster)
with public java.time.LocalTime java.time.LocalTime.with(java.time.temporal.TemporalAdjuster)
with public java.time.LocalTime java.time.LocalTime.with(java.time.temporal.TemporalField,long)
withHour public java.time.LocalTime java.time.LocalTime.withHour(int)
withMinute public java.time.LocalTime java.time.LocalTime.withMinute(int)
withSecond public java.time.LocalTime java.time.LocalTime.withSecond(int)
withNano public java.time.LocalTime java.time.LocalTime.withNano(int)
truncatedTo public java.time.LocalTime java.time.LocalTime.truncatedTo(java.time.temporal.TemporalUnit)
plus public java.time.temporal.Temporal java.time.LocalTime.plus(java.time.temporal.TemporalAmount)
plus public java.time.LocalTime java.time.LocalTime.plus(long,java.time.temporal.TemporalUnit)
plus public java.time.temporal.Temporal java.time.LocalTime.plus(long,java.time.temporal.TemporalUnit)
plus public java.time.LocalTime java.time.LocalTime.plus(java.time.temporal.TemporalAmount)
plusHours public java.time.LocalTime java.time.LocalTime.plusHours(long)
plusMinutes public java.time.LocalTime java.time.LocalTime.plusMinutes(long)
plusSeconds public java.time.LocalTime java.time.LocalTime.plusSeconds(long)
plusNanos public java.time.LocalTime java.time.LocalTime.plusNanos(long)
minus public java.time.temporal.Temporal java.time.LocalTime.minus(long,java.time.temporal.TemporalUnit)
minus public java.time.temporal.Temporal java.time.LocalTime.minus(java.time.temporal.TemporalAmount)
minus public java.time.LocalTime java.time.LocalTime.minus(long,java.time.temporal.TemporalUnit)
minus public java.time.LocalTime java.time.LocalTime.minus(java.time.temporal.TemporalAmount)
minusHours public java.time.LocalTime java.time.LocalTime.minusHours(long)
minusMinutes public java.time.LocalTime java.time.LocalTime.minusMinutes(long)
minusSeconds public java.time.LocalTime java.time.LocalTime.minusSeconds(long)
minusNanos public java.time.LocalTime java.time.LocalTime.minusNanos(long)
adjustInto public java.time.temporal.Temporal java.time.LocalTime.adjustInto(java.time.temporal.Temporal)
until public long java.time.LocalTime.until(java.time.temporal.Temporal,java.time.temporal.TemporalUnit)
atDate public java.time.LocalDateTime java.time.LocalTime.atDate(java.time.LocalDate)
atOffset public java.time.OffsetTime java.time.LocalTime.atOffset(java.time.ZoneOffset)
toSecondOfDay public int java.time.LocalTime.toSecondOfDay()
toNanoOfDay public long java.time.LocalTime.toNanoOfDay()
isAfter public boolean java.time.LocalTime.isAfter(java.time.LocalTime)
isBefore public boolean java.time.LocalTime.isBefore(java.time.LocalTime)
now public static java.time.LocalTime java.time.LocalTime.now(java.time.ZoneId)
now public static java.time.LocalTime java.time.LocalTime.now()
now public static java.time.LocalTime java.time.LocalTime.now(java.time.Clock)
get public int java.time.LocalTime.get(java.time.temporal.TemporalField)
equals public boolean java.time.LocalTime.equals(java.lang.Object)
toString public java.lang.String java.time.LocalTime.toString()
hashCode public int java.time.LocalTime.hashCode()
compareTo public int java.time.LocalTime.compareTo(java.lang.Object)
compareTo public int java.time.LocalTime.compareTo(java.time.LocalTime)
getLong public long java.time.LocalTime.getLong(java.time.temporal.TemporalField)
format public java.lang.String java.time.LocalTime.format(java.time.format.DateTimeFormatter)
query public java.lang.Object java.time.LocalTime.query(java.time.temporal.TemporalQuery)
of public static java.time.LocalTime java.time.LocalTime.of(int,int,int)
of public static java.time.LocalTime java.time.LocalTime.of(int,int,int,int)
of public static java.time.LocalTime java.time.LocalTime.of(int,int)
isSupported public boolean java.time.LocalTime.isSupported(java.time.temporal.TemporalUnit)
isSupported public boolean java.time.LocalTime.isSupported(java.time.temporal.TemporalField)
parse public static java.time.LocalTime java.time.LocalTime.parse(java.lang.CharSequence,java.time.format.DateTimeFormatter)
parse public static java.time.LocalTime java.time.LocalTime.parse(java.lang.CharSequence)
range public java.time.temporal.ValueRange java.time.LocalTime.range(java.time.temporal.TemporalField)
wait public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait public final void java.lang.Object.wait() throws java.lang.InterruptedException
getClass public final native java.lang.Class java.lang.Object.getClass()
notify public final native void java.lang.Object.notify()
notifyAll public final native void java.lang.Object.notifyAll()

Time format hour:minute:second. java.time package classes is based on ISO standards.

import java.time.LocalTime;; //for LocalTime
public class MyDate{
    public static void main(String[] args){
      //Create objects of LocalTime
      LocalTime time = LocalTime.now();

      System.out.println(" Time : "+time);
      System.out.println(" Add 5000 Nanosecond : "+time.plusNanos(5000));
      System.out.println(" Add 60 Second  : "+time.plusSeconds(60));
      System.out.println(" Add 5 Minutes : "+time.plusMinutes(5));
      System.out.println(" Add 10 Hours : "+time.plusHours(10));
      System.out.println(" Minus 1000000 Nanosecond : "+time.minusNanos(1000000));
      System.out.println(" Minus 60 Seconds : "+time.minusSeconds(60));
      System.out.println(" Minus 10 Minutes : "+time.minusMinutes(10));
      System.out.println(" Minus 4 Hours : "+time.minusHours(4));
    }
}
 Time : 07:48:15.611
 Add 5000 Nanosecond : 07:48:15.611005
 Add 60 Second  : 07:49:15.611
 Add 5 Minutes : 07:53:15.611
 Add 10 Hours : 17:48:15.611
 Minus 1000000 Nanosecond : 07:48:15.610
 Minus 60 Seconds : 07:47:15.611
 Minus 10 Minutes : 07:38:15.611
 Minus 4 Hours : 03:48:15.611

Date Time Conversion

Given of year and total days find the date

import java.time.LocalDate; //for LocalDate
public class MyDate{
    public static void main(String[] args){
      
      LocalDate date = LocalDate.ofYearDay(2020,50);
      System.out.println(date); //2020-02-19
    }
}
2020-02-19

Mathematical calculation of current date

import java.time.LocalDate; //for LocalDate
public class MyDate{
    public static void main(String[] args){
      //Create objects of LocalDate
      LocalDate date = LocalDate.now();

      System.out.println("Inital Data : "+date);

      System.out.println("Add 10 Days : "+date.plusDays(10));

      System.out.println("Minus 10 Days : "+date.minusDays(10));

      System.out.println("Add 3 Weeks : "+date.plusWeeks(3));

      System.out.println("Minus 3 Weeks : "+date.minusWeeks(3));

      System.out.println("Add 3 Months : "+date.plusMonths(3));

      System.out.println("Minus 3 Months : "+date.minusMonths(3));

      System.out.println("Add 3 Years : "+date.plusYears(3));

      System.out.println("Minus 3 Years : "+date.minusYears(3));
    }
}
Inital Data : 2019-07-04
Add 10 Days : 2019-07-14
Minus 10 Days : 2019-06-24
Add 3 Weeks : 2019-07-25
Minus 3 Weeks : 2019-06-13
Add 3 Months : 2019-10-04
Minus 3 Months : 2019-04-04
Add 3 Years : 2022-07-04
Minus 3 Years : 2016-07-04

Build a date

import java.time.LocalDate; //for LocalDate
public class MyDate{
    public static void main(String[] args){
      byte day = 12;
      byte month = 2;
      short year =2020;
      LocalDate date = LocalDate.of(year, month, day);
      System.out.println(date);
    }
}
2020-02-12

Convert Second to other format

import java.time.Duration; 
public class MyDate{
    public static void main(String[] args){
      int second = 5000;
      Duration time = Duration.ofSeconds(second);

      System.out.println(time);
      System.out.println("Minutes : "+time.toMinutes());
      System.out.println("Day : "+time.toDays());
      System.out.println("Millisecond : "+time.toMillis());
      System.out.println("Nanosecond : "+time.toNanos());
  }
}
PT1H23M20S
Minutes : 83
Day : 0
Millisecond : 5000000
Nanosecond : 5000000000000

Convert string text to date object

//Java string to date conversion
import java.util.Date;
import java.text.SimpleDateFormat;  
import java.util.Locale; 
import java.text.DateFormat; 
class Execution{
  public int data;
  public static void main(String[] args) {
    //date string text
    String text = "January 26 2020";
    //set pattern according to date text (January 26 2020=MMMM d yyyy)  
    DateFormat format = new SimpleDateFormat("MMMM d yyyy", Locale.ENGLISH);

    try {
     Date myDate = format.parse(text);
     System.out.println(myDate); 
   }
   catch (Exception e) {
     System.out.println(e); 
   }

 }
}

Output

Sun Jan 26 00:00:00 IST 2020

Get interval of two dates

//Java compare two dates difference (intervals)
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.text.SimpleDateFormat;  
import java.util.Locale; 
import java.text.DateFormat; 
class Execution{
  public static long getDifference(Date d1,Date d2,TimeUnit unit){
    //get d
    long milliSeconds = d1.getTime() - d2.getTime();
    return unit.convert(milliSeconds,TimeUnit.MILLISECONDS);

  }
  public static void main(String[] args) {
    //date string text
    String text1 = "January 26 2019 6:15:19";
    String text2 = "August  15 2019 7:18:59";
    String pattern="MMMM d yyyy HH:mm:ss"; //according to text date
    DateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);

    try {
     Date date1 = format.parse(text1);
     Date date2 = format.parse(text2);
     
     long difference=getDifference(date2,date1,TimeUnit.MINUTES);
     long minutes=difference %(60*24); //in case check minutes
     long hours=minutes/60;
     minutes%=60;
     difference/=60*24;
     System.out.println("Days "+difference+" hours :"+ hours +" minutes :"+minutes);
   }

   catch (Exception e) {
     System.out.println(e); 
   }

 }
}

Output


Days 201 hours :1 minutes :3

Change date format in a Java string

//Change date format in a Java string
import java.util.Date;
import java.text.SimpleDateFormat;  
import java.util.Locale; 
import java.text.DateFormat; 
class Execution{
  public static void main(String[] args) {
    //date string text
    String text = "January 12 2020";
    //set pattern according to date text (January 26 2020=MMMM d yyyy)  
    DateFormat pattern = new SimpleDateFormat("MMMM d yyyy", Locale.ENGLISH);

    try {
     Date myDate = pattern.parse(text);
     //modified date format 
     String formatted = new SimpleDateFormat("yyyy-MM-dd").format(myDate);
     System.out.println(myDate);
     System.out.println(formatted); 
   }
   catch (Exception e) {
     System.out.println(e); 
   }

 }
}

Output

Change date format in a Java string
Sun Jan 12 00:00:00 IST 2020
2020-01-12


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved