在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,只需创建一个新的Date()

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2016/11/16 12:08:43

对于java.util.Calendar,使用Calendar.getInstance()

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43

对于java.time.LocalDateTime,使用LocalDateTime.now()

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)); //2016/11/16 12:08:43

对于java.time.LocalDate,使用LocalDate.now()

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
System.out.println(dtf.format(localDate)); //2016/11/16

参考:https://www.mkyong.com/java/java-how-to-get-current-date-time-date-and-calender/

java.util.Date date = new java.util.Date();

它会在实例化时自动填充。

我创建了这种方法,它对我很有用。。。

public String GetDay() {
    return String.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd")));
}

public String GetNameOfTheDay() {
    return String.valueOf(LocalDateTime.now().getDayOfWeek());
}

public String GetMonth() {
    return String.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("MM")));
}

public String GetNameOfTheMonth() {
    return String.valueOf(LocalDateTime.now().getMonth());
}

public String GetYear() {
    return String.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
}

public boolean isLeapYear(long year) {
    return Year.isLeap(year);
}

public String GetDate() {
    return GetDay() + "/" + GetMonth() + "/" + GetYear();
}

public String Get12HHour() {
    return String.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh")));
}

public String Get24HHour() {
    return String.valueOf(LocalDateTime.now().getHour());
}

public String GetMinutes() {
    return String.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("mm")));
}

public String GetSeconds() {
    return String.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("ss")));
}

public String Get24HTime() {
    return Get24HHour() + ":" + GetMinutes();
}

public String Get24HFullTime() {
    return Get24HHour() + ":" + GetMinutes() + ":" + GetSeconds();
}

public String Get12HTime() {
    return Get12HHour() + ":" + GetMinutes();
}

public String Get12HFullTime() {
    return Get12HHour() + ":" + GetMinutes() + ":" + GetSeconds();
}

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

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

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

如前所述,基本的Date()可以根据需要获取当前时间。在我最近大量使用Java Date的经验中,内置类有很多奇怪之处(以及许多Date类方法的弃用)。我觉得有一个奇怪的地方是,月份是基于0指数的,从技术角度来看,这是合理的,但实际情况可能非常令人困惑。

如果你只关心当前的日期,那就足够了——然而,如果你打算用日期进行大量的操作/计算,那么使用第三方库可能会非常有益(因为很多Java开发人员对内置功能不满意,所以有很多这样的库)。

我支持Stephen C的建议,因为我发现Joda time对简化我的日期工作非常有用,它也有很好的文档记录,你可以在整个网站上找到许多有用的例子。我甚至最终编写了一个静态包装类(作为DateUtils),我使用它来合并和简化所有常见的日期操作。