获取当前时间(Date类型)

1
2
3
4
5
6
import java.util.Date;

public static void main(String[] args) {
Date nowDate = new Date();
System.out.println(nowDate);
}

输出:Mon Apr 12 16:09:29 CST 2021

获取当前时间(Instant类型)

1
2
3
4
5
6
7
8
import java.time.Instant;
import java.util.concurrent.TimeUnit;

public static void main(String[] args) {
//东八区当前时间
Instant nowDate = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(nowDate);
}

输出:2021-04-12T16:33:18.334Z

获取当前时间毫秒数(System获取、Date获取、Calendar获取、Instant获取)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public static void main(String[] args) {
//方法一
System.out.println(System.currentTimeMillis());
//方法二
Date date = new Date();
System.out.println(date.getTime());
//方法三
Calendar calendar = Calendar.getInstance();
long time = calendar.getTimeInMillis();
System.out.println(time);
//方法四
Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(now.toEpochMilli());
}

输出:1618217202418 1618217202419 1618217202425 1618246002438

获取当前年月(Calendar类型)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Calendar;

public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
//年
int year = cal.get(Calendar.YEAR);
//月
int month = cal.get(Calendar.MONTH ) + 1;
//日
int day = cal.get(Calendar.DATE);
//拼接
String nowDate = year + "-" + month + "-" + day;
System.out.println(nowDate);
}

输出:2021-4

获取当前年月(Calendar类型),月份自动填0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Calendar;

public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
//年
int year = cal.get(Calendar.YEAR);
//月
int month = cal.get(Calendar.MONTH ) + 1;
String month2 = month<10 ? "0" + month : String.valueOf(month);
//日
int day = cal.get(Calendar.DATE);
//拼接
String nowDate = year + "-" + month2 + "-" + day;
System.out.println(nowDate);
}

输出:2021-04

获取当前日期(LocalDate类型)

1
2
3
4
5
6
import java.time.LocalDate;

public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println(today);
}

输出:2021-04-12

获取当前时间(LocalDateTime类型)

1
2
3
4
5
6
import java.time.LocalDateTime;

public static void main(String[] args) {
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
}

输出:2021-04-12T16:37:25.244

获取当前时间(Timestamp类型)

1
2
3
4
5
6
7
8
9
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String now = sdf.format(timestamp);
System.out.println(now);
}

输出:2021-04-12 17:06:33

Date类型 与 String类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//Date转String
Date date = new Date();
String now = sdf.format(date);
System.out.println(now);
//String转Date
Date now2 = sdf.parse("2021-04-12 17:30:54");
System.out.println(now2);
}

输出:2021-04-12 16:55:59

DateTime类型 转 String类型

1
2
3
4
5
public static void main(String[] args) {
DateTime dateTime = new DateTime();
String nowTime = dateTime.toString("yyyy-MM-dd HH:mm:ss");
System.out.println(nowTime);
}

输出:2021-04-12 16:54:09

Date类型 转 Instant类型

1
2
3
4
5
6
7
8
import java.time.Instant;
import java.util.Date;

public static void main(String[] args) {
Date date = new Date();
Instant instant = date.toInstant();
System.out.println(instant);
}

输出:2021-04-12 16:54:09

DateTime类型 与 Calendar类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Calendar;
import java.util.Date;

public static void main(String[] args) {
//Calendar转化为Date
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
System.out.println(date);
//Date转化为Calendar
Date date2=new Date();
Calendar cal2 = Calendar.getInstance();
cal.setTime(date2);
System.out.println(cal2);
}

输出:Mon Apr 12 17:27:11 CST 2021

输出:java.util.GregorianCalendar[
time=1618219631202,
areFieldsSet=true,
areAllFieldsSet=true,
lenient=true,
zone=sun.util.calendar.ZoneInfo[
id=”Asia/Shanghai”,
offset=28800000,
dstSavings=0,
useDaylight=false,
transitions=29,
lastRule=null
],
firstDayOfWeek=1,
minimalDaysInFirstWeek=1,
ERA=1,
YEAR=2021,
MONTH=3,
WEEK_OF_YEAR=16,
WEEK_OF_MONTH=3,
DAY_OF_MONTH=12,
DAY_OF_YEAR=102,
DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=2,
AM_PM=1,
HOUR=5,
HOUR_OF_DAY=17,
MINUTE=27,
SECOND=11,
MILLISECOND=202,
ZONE_OFFSET=28800000,
DST_OFFSET=0
]

String类型 与 Instant类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public static void main(String[] args) {
//Instant转String
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Instant instant = Instant.now();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
String now = dtf.format(localDateTime);
System.out.println(now);

//String转Instant
String string = "2021-04-12T23:59:59.123Z";
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.FRANCE);
LocalDateTime date = LocalDateTime.parse(string, dtf2);
System.out.println(date);
}

输出:2021-04-12 17:01:33

LocalDate类型 转换 Date类型

1
2
3
4
5
6
7
8
9
10
11
12
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
Date date = Date.from(instant);
System.out.println(date);
}

输出:Mon Apr 12 00:00:00 CST 2021