java format conversion function of time and

java get the current system date and time

1, obtained by the class Date
Date date = new Date();//date为当前系统时间,得到结果如:Sun Nov 03 17:13:33 CST 2019
        date.getHours();//小时,值范围0-23
		date.getDate();//月中的第几日
		date.getDay();//星期几,值范围0-6
		date.getMonth();//月
		date.getYear();//年
		date.getMinutes();//分
		date.getSeconds();//秒
2, obtained by the Calendar class
Calendar calendar = Calendar.getInstance();//实例化对象
Date time = calendar.getTime();//得到系统当前时间,返回类型为Date
int year = c.get(Calendar.YEAR); //年
int month = c.get(Calendar.MONTH); //月
int date = c.get(Calendar.DATE); //日
int hour = c.get(Calendar.HOUR_OF_DAY); //小时,24小时制
int halfHour = c.get(Calendar.HOUR); //小时,12小时制
int minute = c.get(Calendar.MINUTE); //分钟
int second = c.get(Calendar.SECOND); //秒
3, obtained by the class System
long time = System.currentTimeMillis();//currentTimeMillis()为静态方法,通过类名直接调用,得到系统当前时间戳,返回类型为long。
4, Hutool encapsulated method
DateTime testDate = new DateTime();//当前时间,DateTime继承自Date
Date date = DateUtil.date();//当前时间
Date date2 = DateUtil.date(Calendar.getInstance());//当前时间
Date date3 = DateUtil.date(System.currentTimeMillis());//当前时间
DateUtil.year(date);//获得月份,从0开始计数
DateUtil.month(date);//获得月份枚举
DateUtil.monthEnum(date);
5, the class obtained by LocalDateTime
LocalDateTime dateTime = LocalDateTime.now();//2019-01-01T14:05:20.777
dateTime.getYear();//年
dateTime.getMonthValue();//月
dateTime.getDayOfMonth();//日
dateTime.getHour();//时
dateTime.getMinute();//分
dateTime.getSecond();//秒
6, available through Instant
Instant timestamp = Instant.now();//2019-01-01T06:15:22.581Z

Java time format conversion

java.text package

Time format conversion classes SimpleDateFormat

SimpleDateFormat DateFormat class inherits from class DateFormat class is an abstract class, inherited from the Format category.

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String changeDate = format.format(date);//Date类型转换成String类型
//编译时异常,要显示处理
try {
        Date date1 = format.parse("2019-11-06 09:05:56");//String类型转换成Date类型
    } catch (ParseException e) {
        e.printStackTrace();
    }

Wherein the instantiation parameters of the format SimpleDateFormat customizable, and the parameters corresponding letter meanings indicated in the following table.

letter meaning
Y year
M In the first few months
w The first few weeks of the year
W The first few weeks of the month
D The first few days of the year
d The first day of the month
F The week of the month
E Days of the week
h am (noon) / pm hours (afternoon) in (1-12)
H The number of hours of the day (0-23)
k The number of hours of the day (1-24)
K am (noon) / pm hours (afternoon) in (0-11)
m minute
s second
S millisecond

Hutool java tool library

DateUtil

Type Conversion Tools -Convert

== Hutool change tools in the development document describes a method includes, but is not included in the source code, you can not use ==

String a= "2019-1-1";
Date date = Convert.toDate(a);

The date and time tool -DateUtil

Automatically identify the format
YYYY the MM-dd-HH: mm: SS
YYYY the MM-dd-
HH: mm: SS
YYYY the MM-dd-HH: mm
YYYY the MM-dd-HH: mm: ss.SSS

String dateStr = "2019-01-01";
Date date = DateUtil.parse(dateStr);//String转换为Date
String format = DateUtil.format(date, "yyyy/MM/dd");//Date类型转换成String类型
String formatDate = DateUtil.formatDate(date);//常用格式的格式化,结果:22019-01-01
Published 24 original articles · won praise 1 · views 2442

Guess you like

Origin blog.csdn.net/qq_35018214/article/details/102884412