java Date get today date

www.lautt‮moc.iru‬
java Date get today date

In Java, you can use the java.util.Calendar class to get the current date and time, and the java.util.Date class to represent a specific moment in time.

Here is an example of how to get the current date using the Calendar and Date classes in Java:

import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    // Get the current date and time using the Calendar class
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    System.out.println("Current date: " + year + "-" + (month+1) + "-" + day);  // Output: current date

    // Get the current date and time using the Date class
    Date now = new Date();
    System.out.println("Current date and time: " + now);  // Output: current date and time
  }
}

In this example, we use the Calendar.getInstance method to get a Calendar instance representing the current date and time, and then use the get method to extract the year, month, and day from the Calendar. We also create a Date object using the new Date() constructor, which represents the current date and time.

You can use the Calendar and Date classes to manipulate and format the current date and time as needed. For example, you can use the set method to change the date or time, or the java.text.SimpleDateFormat class to format the date and time as a string.

Created Time:2017-11-03 00:22:23  Author:lautturi