How to get the time in java and convert it to a standard time format string, and create a time object with the time character
One, get the time string
I sum it up as three steps
Step 1: Get the current time
Step 2: Create a time format string and get the local time zone
Step 3: Use SimpleDateFormat to convert the time format The following shows some 内联代码片
.
//获取现在时间
Date date = new Date();
System.out.println(date);
//创建时间格式字符串
String pattern = "yyyy-MM-dd HH:mm:ss";
//获取当地时区
Locale locale = Locale.getDefault();
//利用SimpleDateFormat 进行时间格式的转换
SimpleDateFormat sdf = new SimpleDateFormat(pattern,locale);
String time = sdf.format(date);
System.out.println(time);
Create time object
The first step: give the time string
Step 2: Create a time format string and get the local time zone
Step 3: Use SimpleDateFormat to convert the time format The following shows some 内联代码片
.
Show some below 内联代码片
.
//创建时间字符串
String time = "2021-1-21 12:12:12";
String parrte = "yyyy-MM-dd HH:mm:ss";
Locale locale = Locale.getDefault();
//利用SimpleDateFormat 进行时间转换
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(parrte,locale);
//输出的时间 返回值为Date
Date date = simpleDateFormat.parse(time);
System.out.println(date);