在Java中获取当前日期/时间的最佳方法是什么?


当前回答

我会继续回答这个问题,因为当我有同样的问题时,这就是我所需要的:

Date currentDate = new Date(System.currentTimeMillis());

currentDate现在是Java date对象中的当前日期。

其他回答

(注意:仅适用于Java版本<8。对于Java 8+,请检查其他回复。)

如果您只需要输出格式为YYYY.MM.DD-HH.MM.SS(非常常见的情况)的时间戳,那么下面是方法:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());

在Java 8中,它是:

LocalDateTime.now()

如果您需要时区信息:

ZonedDateTime.now()

如果您想打印花式格式化字符串:

System.out.println(ZonedDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME))

只需尝试以下代码:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CurrentTimeDateCalendar {
    public static void getCurrentTimeUsingDate() {
        Date date = new Date();
        String strDateFormat = "hh:mm:ss a";
        DateFormat dateFormat = new SimpleDateFormat(strDateFormat);
        String formattedDate= dateFormat.format(date);
        System.out.println("Current time of the day using Date - 12 hour format: " + formattedDate);
    }
    public static void getCurrentTimeUsingCalendar() {
        Calendar cal = Calendar.getInstance();
        Date date=cal.getTime();
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        String formattedDate=dateFormat.format(date);
        System.out.println("Current time of the day using Calendar - 24 hour format: "+ formattedDate);
    }
}

其样本输出为:

使用日期-12小时格式的当前时间:11:13:01 PM使用日历的当前时间-24小时格式:23:13:01

更多信息:

在Java中获取当前日期时间

新的数据时间API是随着Java8的诞生而引入的。这是到期的以下是旧数据时间API中导致的问题。难以处理时区:需要编写大量代码来处理时区。非线程安全:java.util.Date不是线程安全的。

所以,看看Java 8吧

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


public class DataTimeChecker {

    public static void main(String args[]) {
        DataTimeChecker dateTimeChecker = new DataTimeChecker();
        dateTimeChecker.DateTime();
    }

    public void DateTime() {
        // Get the current date and time
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("Current DateTime: " + currentTime);

        LocalDate date1 = currentTime.toLocalDate();
        System.out.println("Date : " + date1);

        Month month = currentTime.getMonth();
        int day = currentTime.getDayOfMonth();
        int seconds = currentTime.getSecond();

        System.out.println("Month : " + month);
        System.out.println("Day : " + day);
        System.out.println("Seconds : " + seconds);

        LocalDateTime date2 = currentTime.withDayOfMonth(17).withYear(2018);
        System.out.println("Date : " + date2);

        //Prints 17 May 2018
        LocalDate date3 = LocalDate.of(2018, Month.MAY, 17);
        System.out.println("Date : " + date3);

        //Prints 04 hour 45 minutes
        LocalTime date4 = LocalTime.of(4, 45);
        System.out.println("Date : " + date4);

        // Convert to a String 
        LocalTime date5 = LocalTime.parse("20:15:30");
        System.out.println("Date : " + date5);
    }
}

上述编码的输出:

Current DateTime: 2018-05-17T04:40:34.603
Date : 2018-05-17
Month : MAY
Day : 17
Seconds : 34
Date : 2018-05-17T04:40:34.603
Date : 2018-05-17
Date : 04:45
Date : 20:15:30

首先了解java.util.Date类

1.1如何获取当前日期

import java.util.Date;

class Demostration{
    public static void main(String[]args){
        Date date = new Date(); // date object
        System.out.println(date); // Try to print the date object
    }
}

1.2如何使用getTime()方法

import java.util.Date;
public class Main {
    public static void main(String[]args){
        Date date = new Date();
        long timeInMilliSeconds = date.getTime();
        System.out.println(timeInMilliSeconds);
    }
}

这将返回自1970年1月1日00:00:00 GMT以来的毫秒数,用于时间比较。

1.3如何使用SimpleDateFormat类设置时间格式

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

class Demostration{
    public static void main(String[]args){
        Date date=new Date();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate=dateFormat.format(date);
        System.out.println(formattedDate);
    }
}

也可以尝试使用不同的格式模式,如“yyyy-MM-dd hh:MM:ss”,并选择所需的模式。http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

了解java.util.Calendar类

2.1使用日历类获取当前时间戳

import java.util.Calendar;

class Demostration{
    public static void main(String[]args){
        Calendar calendar=Calendar.getInstance();
        System.out.println(calendar.getTime());
    }
}

2.2尝试使用setTime和其他设置方法将日历设置为不同的日期。

资料来源:http://javau91.blogspot.com/