获取当前系统时间的几种方式(返回毫秒数)
- new Date().getTime();
- System.currentTimeMillis();
- Calendar.getInstance().getTimeInMills();
- Clock.systemDefaultZone().millis();//这是JDK8引入的
import java.time.Clock;
import java.util.Calendar;
import java.util.Date;
/** 1. Created by dllo */
public class DateTest {
public static void main(String[] args) {
Date date = new Date();
long time1 = date.getTime();
System.out.println("第一种方式: "+time1);
long time2 = System.currentTimeMillis();
System.out.println("第二种方式: "+time2);
Calendar calendar = Calendar.getInstance();
long time3 = calendar.getTimeInMillis();
System.out.println("第三种方式: "+time3);
long time4 = Clock.systemDefaultZone().millis();
System.out.println("第四种方式: "+time4);
}
}
- 使用java.text.SimpleDateFormat下的format()方法;
- 使用java.time.format.DateTimeFormatter下的format()方法://JDK8引入的
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/** * Created by dllo */
public class DateTest1 {
public static void main(String[] args) {
System.out.println(formatTime(System.currentTimeMillis()));
System.out.println(formatTime1());
}
public static String formatTime(long time) {
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String strTime = sdf.format(date);
return strTime;
}
public static String formatTime1(){
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String strTime = dtf.format(localDateTime);
return strTime;
}
}
时间日期的解析prase()
- 使用SimpleDateFormat下的parse()
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** * Created by dllo */
public class DateTest2 {
public static void main(String[] args) {
System.out.println(parseTime("2018-04-08 07:50:38"));
}
public static long parseTime(String strTime){
long time = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = sdf.parse(strTime);
time = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
}
获取年,月,日,时,分,秒
Calendar calendar = Calendar.getInstance()
System.out.println(calendar.get(Calendar.YEAR))
System.out.println(calendar.get(Calendar.MONTH))
System.out.println(calendar.get(Calendar.DATE))
System.out.println(calendar.get(Calendar.HOUR_OF_DAY))
System.out.println(calendar.get(Calendar.MINUTE))
System.out.println(calendar.get(Calendar.SECOND))
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class DateMain1 {
public static void main(String[] args){
LocalDateTime localDate = LocalDateTime.now();
System.out.println(localDate);
LocalDate localDate1 = LocalDate.of(2000,11,11);
System.out.println(localDate1);
int year = localDate.getYear();
System.out.println(year);
int month = localDate.getMonthValue();
System.out.println(month);
int day = localDate.getDayOfMonth();
System.out.println(day);
int hour = localDate.getHour();
System.out.println(hour);
int minute = localDate.getMinute();
System.out.println(minute);
int second = localDate.getSecond();
System.out.println(second);
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println(dayOfWeek);
LocalDateTime newLocalDate = localDate.plusMonths(12);
System.out.println(newLocalDate);
LocalDateTime newLocalDate1 = localDate.minusMonths(12);
System.out.println(newLocalDate1);
}
}
Java 8 推出了全新的日期时间API
- LocalDate:本地日期,不包含具体时间 例如:2014-01-14 可以用来记录生日、纪念日、加盟日等。
- LocalTime:本地时间,不包含日期。
- LocalDateTime:组合了日期和时间,但不包含时差和时区信息。
- ZonedDateTime:最完整的日期时间,包含时区和相对UTC或格林威治的时差。
import java.time.*;
/** * Created by dllo */
public class DateTest2 {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
int year = localDate.getYear();
System.out.println(year);
int month = localDate.getMonthValue();
System.out.println(month);
int day = localDate.getDayOfMonth();
System.out.println(day);
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println(dayOfWeek);
LocalDate newLocalDate = localDate.plusMonths(12);
System.out.println(newLocalDate);
LocalDate newLocalDate1 = localDate.minusMonths(12);
System.out.println(newLocalDate1);
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
int hour = localTime.getHour();
System.out.println(hour);
int minute = localTime.getMinute();
System.out.println(minute);
int second = localTime.getSecond();
System.out.println(second);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
}
}