[Java] How to use the Calendar class

Programming study diary

October 29, 2020 Since I dealt with the Calendar class in Java, I will briefly summarize how to use it.

What is the Calendar class?

A class for manipulating dates and specified dates and times. Used to calculate, get, and set the date and time in Java.

In the Calendar class, create an object by calling the getInstance method instead of the operator new. The getInstance method returns an object initialized to the current date and time each time it is called. When using it, be sure to specify ʻimport java.util.Calendar`.

Sample code

import java.util.Calendar

public class Main {
  public static void main(String[] args) throws Exception {
    Calendar calendar = Calendar.getInstance();
    System.out.println(calendar.get(Calendar.YEAR) + "Year" + calendar.get(Calendar.MONTH)+ "Moon" + calendar.get(Calendar.DATE) + "Day" );
  }
}

Execution result


October 29, 2020

How to use basic methods

set method

The set method is used to set the date and time. Therefore, it is often used in programs that process dates.

Note that in the example below, the previous values will remain except for the specified element (years in this case).

How to use


import java.util.Calendar

public class Main {
  public static void main(String[] args) throws Exception {
    Calendar calendar = Calendar.getInstance();
    System.out.println("Current date and time:" + calendar.getTime().toString());

    //Set the year to 2022
    calendar.set(Calendar.YEAR, 2022);
    System.out.println("Set 2022 for the year:" + calendar.getTime());
  }
}

Execution result


Current date and time: Thu Oct 29 10:43:58 UTC 2020
Set the year to 2022: Thu Oct 29 10:43:58 UTC 2022

add method

Use the add method when adding or subtracting dates in the Calendar class.

How to use


import java.util.Calendar

public class Main {
  public static void main(String[] args) throws Exception {
    Calendar calendar = Calendar.getInstance();
    System.out.println("Current date and time:" + calendar.getTime().toString());

    //Add 1 year
    calendar.add(Calendar.YEAR, 1);
    System.out.println("Increase by one year:" + calendae.getTime());
  }
}

Execution result


Current date and time: Thu Oct 29 10:43:58 UTC 2020
Increase by 1 year: Thu Oct 29 10:43:58 UTC 2021

format method

You can specify the date format with a character string.

import java.text.SimpleDateFormat;
import java.util.Calendar;
 
public class Main {
  public static void main(String[] args) {
     Calendar calendar = Calendar.getInstance();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day");
    System.out.println(sdf.format(calendar.getTime())); 
  }
}

Execution result


October 20, 2020

References

[Java] How to use the Calendar class that can be used to calculate and get the date and time! [Introduction to Java] Complete explanation of how to use the Calendar class (set / add / format) [Introduction to Java] Specify the date format of Calendar (format) [Introduction to Java] How to use Calendar (add)

Recommended Posts

[Java] How to use the Calendar class
[Java] How to use the HashMap class
[Processing × Java] How to use the class
How to use java class
[Java] How to use Calendar class and Date class
How to use the wrapper class
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Java] How to use Math class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Java] How to use Map
[Java] How to use FileReader class and BufferedReader class
[Java] How to use Map
[Java] How to use Thread.sleep to pause the program
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use Java Map
How to use the replace () method (Java Silver)
How to use Java variables
[Java] How to use Optional ①
[Java] How to use compareTo method of Date class
How to use Java HttpClient (Get)
How to use the form_with method
How to disassemble Java class files
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[JavaFX] [Java8] How to use GridPane
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
How to use Java enum type
How to get the class name / method name running in Java
How to make a Java calendar Summary
Multilingual Locale in Java How to use Locale
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
How to install the legacy version [Java]
How to get the date in java
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
To use the "java" command line tool ... How to avoid popping up
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~
How to use Map
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select