Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java date-time method


May 10, 2021 Java


Table of contents


Java date-time method


The method names in the Java Date-Time API are as consistent as possible between classes.

For example, now method returns the date or time value of the current moment. from method allows you to convert from one class to another.

The following table lists common prefixes:

  • of
    Static factory method
    The factory method creates an instance and validates the input parameters
  • from
    The static factory method converts input parameters to instances of the target class.
  • parse
    Static factory method
    Resolve the input string to create an instance of the target class.
  • format
    The instance method
    Format temporary objects to produce strings.
  • get
    The instance method
    Returns a portion of the target object.
  • is
    The instance method
    Query the target object.
  • with
    The instance method
    Returns a copy of the target object for which an element has changed.
  • plus
    The instance method
    Returns a copy of the object with the amount of time added.
  • minus
    The instance method
    Returns a copy of the object with the amount of time subtracted.
  • to
    The instance method
    Convert this object to another type.
  • at
    The instance method
    Combine this object with another object.

Of() method

The XXX() method of the Java Date Time API is used to create objects.

The following code shows how to create objects for the LocalDate class:

import java.time.LocalDate;
import java.time.Month;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate1  = LocalDate.of(2014, 5, 21); 
    System.out.println(localDate1);
    
    LocalDate localDate2  = LocalDate.of(2014, Month.MARCH, 4);
    System.out.println(localDate2);
    
    LocalDate localDate3  = LocalDate.ofEpochDay(2014);
    System.out.println(localDate3);
    
    LocalDate localDate4  = LocalDate.ofYearDay(2014, 39);
    System.out.println(localDate4);
  }
}

The code above produces the following results.

Java date-time method

From() method

from() is a static factory method that derives datetime objects from specified parameters.

Unlike of() from() data conversion of the specified parameters.

The following code shows how to derive LocalDate from LocalDateTime:

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

public class Main {
  public static void main(String[] args) {
    LocalDateTime  localDateTime = LocalDateTime.of(2015, 6,  21,  13, 40);
    System.out.println(localDateTime);
    
    LocalDate localDate  = LocalDate.from(localDateTime);
    System.out.println(localDate);
  }
}

The code above produces the following results.

Java date-time method

With() method

To change the fields in the datetime object, we can use a method with a prefix.

The withXXX() method returns a copy of an object, and the specified field has changed because most objects in the Date Time API are imm changed.

The following code shows how to get LocalDate from another LocalDate and change the year:

import java.time.LocalDate;
import java.time.Month;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate1  = LocalDate.of(2014, Month.MAY,  2);
    System.out.println(localDate1);
    
    LocalDate localDate2  = localDate1.withYear(2015);
    System.out.println(localDate2);
    
    LocalDate localDate3  = localDate1.withYear(2014).withMonth(7);
    System.out.println(localDate3);
    
  }
}

The code above produces the following results.

Java date-time method

GetXXX() method

getXXX() specified element of the object.

The following code shows how to LocalDate and days from the LocalDate object:

import java.time.LocalDate;
import java.time.Month;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate = LocalDate.of(2014, 6, 21);
    int year = localDate.getYear();
    System.out.println(year);
    Month month = localDate.getMonth();
    System.out.println(month);

    int day = localDate.getDayOfMonth();
    System.out.println(day);

  }
}

The code above produces the following results.

Java date-time method

ToXXX() method

toXXX() objects to related types.

The following code shows some examples of using the toXXX() method.

import java.time.LocalDate;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate = LocalDate.of(2014, 6, 21); 
    long days = localDate.toEpochDay(); 
    System.out.println(days);

  }
}

The code above produces the following results.

Java date-time method

AtXXX() method

atXXX() a new datetime object from an existing datetime object with additional information.

The following code is used in methods to add additional information to the date object.

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

public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, 6, 21);
    System.out.println(localDate);

    LocalDateTime  localTime1 = localDate.atStartOfDay();
    System.out.println(localTime1);

    LocalDateTime  localTime2 = localDate.atTime(16, 21);
    System.out.println(localTime2);
  }
}

The code above produces the following results.

Java date-time method

The following code shows how to use the atXXX() method of building local dates using the support builder pattern:

import java.time.LocalDate;
import java.time.Year;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = Year.of(2014).atMonth(6).atDay(21);
    System.out.println(localDate);

  }
}

The code above produces the following results.

Java date-time method

PlusXXX() method

plusXXX() a copy of the object by adding the specified value.

The following code shows how to use the plus method to add more time date objects locally.

import java.time.LocalDate;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, 6, 21); 
    LocalDate localDate1  = localDate.plusDays(5);   
    System.out.println(localDate1);
    LocalDate localDate2  = localDate.plusMonths(3);
    System.out.println(localDate2);
    LocalDate localDate3  = localDate.plusWeeks(3);        
    System.out.println(localDate3);
  }
}

The code above produces the following results.

Java date-time method

minusXXX() method

minusXXX() a copy of the object by subtracting the specified value.

The following code shows how to subtract time from the local date object.

import java.time.LocalDate;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, 6, 21); 
    LocalDate localDate1  = localDate.minusMonths(5);   
    System.out.println(localDate1);
    LocalDate localDate2  = localDate.minusWeeks(3);
    System.out.println(localDate2);
  }
}

The code above produces the following results.

Java date-time method

Now() method

The now() method returns the current time of various classes, such as LocalDate, LocalTime, LocalDateTime, ZonedDateTime.

The following code shows how to use the now() method to return the current date and time.

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

public class Main {
  public static void main(String[] args) {
    LocalDate localDate = LocalDate.now(); 
    System.out.println(localDate);
    
    LocalTime  localTime  = LocalTime.now();
    System.out.println(localTime);
    
    LocalDateTime  dateTime  = LocalDateTime.now();
    System.out.println(dateTime);
    
    ZonedDateTime dateTimeWithZone  = ZonedDateTime.now();
    System.out.println(dateTimeWithZone);
  }
}

The code above produces the following results.

Java date-time method