Java – 当前日期和时间
在Java中,有一个称为 Date 类的内置类,我们可以导入Java.time 包来使用日期和时间 API。在这里,我们应该打印当前日期和时间。可以有多种方法来打印当前日期和时间。
获取当前日期和时间的不同方法
- 使用日期类
- 使用 Calendar 类的 get() 方法
- 使用日历和格式化程序类以特定格式打印当前日期。
方法一:使用日期类
例子:
Java
// Java Program to Display Current Date and Time
// Using Date class
// Importing required classes
import java.util.*;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of date class
Date d1 = new Date();
// Printing the value stored in above object
System.out.println("Current date is " + d1);
}
}
Java
// Java Program to Illustrate getinstance() Method
// of Calendar Class
// Importing required classes
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of Calender class
Calendar c = Calendar.getInstance();
// Print corresponding instances by passing
// required some as in arguments
System.out.println("Day of week : "
+ c.get(Calendar.DAY_OF_WEEK));
System.out.println("Day of year : "
+ c.get(Calendar.DAY_OF_YEAR));
System.out.println("Week in Month : "
+ c.get(Calendar.WEEK_OF_MONTH));
System.out.println("Week in Year : "
+ c.get(Calendar.WEEK_OF_YEAR));
System.out.println(
"Day of Week in Month : "
+ c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("Hour : "
+ c.get(Calendar.HOUR));
System.out.println("Minute : "
+ c.get(Calendar.MINUTE));
System.out.println("Second : "
+ c.get(Calendar.SECOND));
System.out.println("AM or PM : "
+ c.get(Calendar.AM_PM));
System.out.println("Hour (24-hour clock) : "
+ c.get(Calendar.HOUR_OF_DAY));
}
}
Java
// Java Program to Demonstrate Working of SimpleDateFormat
// Class
// Importing required classes
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
// Class
class GFG {
// Main driver method
public static void main(String args[])
throws ParseException
{
// Formatting as per given pattern in the argument
SimpleDateFormat ft
= new SimpleDateFormat("dd-MM-yyyy");
String str = ft.format(new Date());
// Printing the formatted date
System.out.println("Formatted Date : " + str);
// Parsing a custom string
str = "02/18/1995";
ft = new SimpleDateFormat("MM/dd/yyyy");
Date date = ft.parse(str);
// Printing date as per parsed string on console
System.out.println("Parsed Date : " + date);
}
}
输出
Current date is Thu Mar 31 01:14:28 UTC 2022
方法二:使用 Calendar 类的 get() 方法
getInstance() 方法通常用于获取时间、日期或任何需要的属于日历年份的一些内容。
Tip: Whenever we require anyrhing belonging to Calender, Calender class is one of naive base for sure approach to deal with dae and time instances.
例子:
Java
// Java Program to Illustrate getinstance() Method
// of Calendar Class
// Importing required classes
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of Calender class
Calendar c = Calendar.getInstance();
// Print corresponding instances by passing
// required some as in arguments
System.out.println("Day of week : "
+ c.get(Calendar.DAY_OF_WEEK));
System.out.println("Day of year : "
+ c.get(Calendar.DAY_OF_YEAR));
System.out.println("Week in Month : "
+ c.get(Calendar.WEEK_OF_MONTH));
System.out.println("Week in Year : "
+ c.get(Calendar.WEEK_OF_YEAR));
System.out.println(
"Day of Week in Month : "
+ c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("Hour : "
+ c.get(Calendar.HOUR));
System.out.println("Minute : "
+ c.get(Calendar.MINUTE));
System.out.println("Second : "
+ c.get(Calendar.SECOND));
System.out.println("AM or PM : "
+ c.get(Calendar.AM_PM));
System.out.println("Hour (24-hour clock) : "
+ c.get(Calendar.HOUR_OF_DAY));
}
}
输出:
Day of week : 7
Day of year : 83
Week in Month : 4
Week in Year : 12
Day of Week in Month : 4
Hour : 6
Minute : 22
Second : 48
AM or PM : 1
Hour (24-hour clock) : 18
方法 3:使用日历和格式化程序类以特定格式打印当前日期。
例子:
Java
// Java Program to Demonstrate Working of SimpleDateFormat
// Class
// Importing required classes
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
// Class
class GFG {
// Main driver method
public static void main(String args[])
throws ParseException
{
// Formatting as per given pattern in the argument
SimpleDateFormat ft
= new SimpleDateFormat("dd-MM-yyyy");
String str = ft.format(new Date());
// Printing the formatted date
System.out.println("Formatted Date : " + str);
// Parsing a custom string
str = "02/18/1995";
ft = new SimpleDateFormat("MM/dd/yyyy");
Date date = ft.parse(str);
// Printing date as per parsed string on console
System.out.println("Parsed Date : " + date);
}
}
输出
Formatted Date : 31-03-2022
Parsed Date : Sat Feb 18 00:00:00 UTC 1995