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


当前回答

只需尝试以下代码:

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中获取当前日期时间

其他回答

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

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();
}

Use:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd::HH:mm:ss");
System.out.println(sdf.format(System.currentTimeMillis()));

print语句将打印调用它的时间,而不是创建SimpleDateFormat的时间。因此,可以在不创建任何新对象的情况下重复调用它。

我认为这是最好的方法:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); // 2014/08/06 16:00:22
import java.util.*;
import java.text.*;

public class DateDemo {
   public static void main(String args[]) {
      Date dNow = new Date( );
      SimpleDateFormat ft = 
      new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
      System.out.println("Current Date: " + ft.format(dNow));
   }
}

你可以用数据来表示场效应晶体管的电流数据。所以使用SimpleDateFormat获取格式

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

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

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