Java⽇期时间各种格式类型转换获取当前时间(Date类型)
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类型)
import java.time.Instant;
import urrent.TimeUnit;
public static void main(String[] args) {
/
/东⼋区当前时间
Instant nowDate = w().plusMillis(Millis(8));
System.out.println(nowDate);
}
输出:2021-04-12T16:33:18.334Z
获取当前时间毫秒数(System获取、Date获取、Calendar获取、Instant获取)
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import urrent.TimeUnit;
public static void main(String[] args) {
/
/⽅法⼀
System.out.println(System.currentTimeMillis());
//⽅法⼆
Date date = new Date();
string转date的方法System.out.Time());
//⽅法三
Calendar calendar = Instance();
long time = TimeInMillis();
System.out.println(time);
//⽅法四
Instant now = w().plusMillis(Millis(8));
System.out.EpochMilli());
}
输出:1618217202418    1618217202419    1618217202425    1618246002438
获取当前年⽉(Calendar类型)
import java.util.Calendar;
public static void main(String[] args) {
Calendar cal = Instance();
//年
int year = (Calendar.YEAR);
//⽉
int month = (Calendar.MONTH ) + 1;
/
/⽇
int day = (Calendar.DATE);
//拼接
String nowDate = year + "-" + month + "-" + day;
System.out.println(nowDate);
}
输出:2021-4
获取当前年⽉(Calendar类型),⽉份⾃动填0 import java.util.Calendar;
public static void main(String[] args) {
Calendar cal = Instance();
//年
int year = (Calendar.YEAR);
//⽉
int month = (Calendar.MONTH ) + 1;
String month2 = month<10 ? "0" + month : String.valueOf(month); //⽇
int day = (Calendar.DATE);
//拼接
String nowDate = year + "-" + month2 + "-" + day;
System.out.println(nowDate);
}
输出:2021-04
获取当前⽇期(LocalDate类型)
import java.time.LocalDate;
public static void main(String[] args) {
LocalDate today = w();
System.out.println(today);
}
输出:2021-04-12
获取当前时间(LocalDateTime类型)
import java.time.LocalDateTime;
public static void main(String[] args) {
LocalDateTime today = w();
System.out.println(today);
}
输出:2021-04-12T16:37:25.244
获取当前时间(Timestamp类型)
import java.sql.Timestamp;
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类型转换
ParseException;
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类型
public static void main(String[] args) {
DateTime dateTime = new DateTime();
String nowTime = String("yyyy-MM-dd HH:mm:ss");
System.out.println(nowTime);
}
输出:2021-04-12 16:54:09
Date类型转 Instant类型
import java.time.Instant;
import java.util.Date;
public static void main(String[] args) {
Date date = new Date();
Instant instant = Instant();
System.out.println(instant);
}
输出:2021-04-12 16:54:09 DateTime类型与 Calendar类型转换
import java.util.Calendar;
import java.util.Date;
public static void main(String[] args) {
/
/Calendar转化为Date
Calendar cal = Instance();
Date date = Time();
System.out.println(date);
//Date转化为Calendar
Date date2=new Date();
Calendar cal2 = Instance();
cal.setTime(date2);
System.out.println(cal2);
}
String类型与 Instant类型转换
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 = w();
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类型
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public static void main(String[] args) {
LocalDate localDate = w();
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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。