java calendar to date

java calendar to date

To convert an instance of the 'Calendar' class to an instance of the 'Date' class in Java, you can use the 'getTime' method of the 'Calendar' class. The 'Calendar' class is an abstract class that represents a calendar and provides methods for manipulating and formatting dates and times, while the 'Date' class is a class that represents a specific date and time.

Here is an example of how to convert an instance of the 'Calendar' class to an instance of the 'Date' class:

refer ‮:ot‬lautturi.com
import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    // Creates a Calendar instance with the current date
    Calendar calendar = Calendar.getInstance();

    // Converts the Calendar instance to a Date instance
    Date data = calendar.getTime();

    // Prints the data
    System.out.println(data);
  }
}

This code creates an instance of the 'Calendar' class using the static 'getInstance' method and then uses the 'getTime' method to convert the instance to an instance of the 'Date' class. Finally, it prints the date using the 'toString' method of the 'Date' class.

Note that from Java 8, we recommend using the Java Date and Time API instead of the 'Calendar' class and the 'Date' class because they are simpler and more intuitive to use. For example, to get the current date and convert to an instance of the 'LocalDate' class, just use the static 'now' method of the 'LocalDate' class.

Created Time:2017-11-03 00:14:47  Author:lautturi