java acquired current date and time (compare methodologies)

I. Introduction

Use the time category are:

System.currentTimeMillis():

System.currentTimeMillis () generates a current of milliseconds, this is actually milliseconds since January 1970 when the number of milliseconds 1 0, type Long;
a Date:

Class Daterepresents a specific moment, with millisecond precision.

Start JDK 1.1, the use should be Calendarbased conversion between date and time fields, use the DateFormatclass to formatting and parsing date string. DateCorresponding method obsolete.
Calendar:

CalendarClass is an abstract class, which as a specific instant and a set of YEAR, MONTH, DAY_OF_MONTH, HOURand other 日历字段conversion methods provided between, and for manipulating the calendar fields (for example, obtained date next week) provides a number of methods;

Mainly use the get method:

get(int field)	返回给定日历字段的值。

Which passed the main parameters are:

HOUR_OF_DAY		一天中某个小时;

DAY_OF_WEEK		一个星期中的某天;
DAY_OF_MONTH	一个月中的某天;
DAY_OF_YEAR		当前年中的天数;

WEEK_OF_MONTH	当前月中的星期数;
WEEK_OF_YEAR	当前年中的星期数;

Second, the use

1, obtaining the current time milliseconds

System.currentTimeMillis()

long timeNow = System.currentTimeMillis();
System.out.println(timeNow);

Date

Date date = new Date();
long timeNow = date.getTime();
System.out.println(timeNow);

2, get the current time

System.currentTimeMillis()

long time = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd:HH-mm-ss");
String day = dateFormat.format(time);
System.out.println(day);

Date

Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd:HH-mm-ss");
String day = dateFormat.format(date);
System.out.println(day);

3, to get the current date (relative to the month)

long timeMillis = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("dd");
String dayOfMonth = format.format(timeMillis);
System.out.println(dayOfMonth);

Calendar

Calendar instance = Calendar.getInstance();
int dayOfMonth = instance.get(Calendar.DAY_OF_MONTH);
System.out.println(dayOfMonth);

Three, JDK1.8 the Date / Time API using

Java 8 provides a new API, to replace java.util.Date and java.util.Calendar. Date / Time API provides a plurality of classes, comprising:

  • LOCALDATE
  • LocalTime
  • localdateti to
  • ZonedDateTime

1 LOCALDATE

LocalDate just a date, there is no time. This means that we can only get the current date, but no specific time of day.

LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd");
String date = localDate.format(dateTimeFormatter);
System.out.println(dateTime);2020-01-11

The results are:

2020-01-11

2、LocalTime

LocalTime and LocalDate contrary, it represents a time, no date. This means that we can only get the current time of day, rather than the actual date:

LocalTime localTime = LocalTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String time = localTime.format(dateTimeFormatter);
System.out.println(time);

The results are:

14:20:43

3 localdateti to

The last one is the LocalDateTime, is most commonly used in Java Date / Time classes representative of a combination of the former two classes - i.e., the date and time value:

LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd:HH:mm:ss");
String dateTime = localDateTime.format(dateTimeFormatter);
System.out.println(dateTime);

The results are:

2020-01-11:14:22:37
Published 51 original articles · won praise 43 · views 4173

Guess you like

Origin blog.csdn.net/weixin_44624117/article/details/103937041