java import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeToString { public static void main(String[] args) { // 创建一个LocalDateTime对象 LocalDateTime dateTime = LocalDateTime.now(); // 创建一个DateTimeFormatter对象,定义日期时间的格式 DateTimeFormatter forma...
LocalDateTime parsedDateTime = LocalDateTime.parse("2022-01-01 12:30:00", formatter); 类似的Demo如下: import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeExample { public static void main(String[] args) { //获取当前日期时间 LocalDateTime currentDateTime...
1.LocalDateTime获取毫秒数 代码语言:javascript 代码运行次数:0 //获取秒数Long second=LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));//获取毫秒数Long milliSecond=LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); 2.LocalDateTime与String互转 代码语言:javascript 代码运行次...
3. LocalDateTime转String 同样,我们可以使用DateTimeFormatter类将LocalDateTime类型的日期对象格式化为字符串。String dateTimeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("当前字符串日期时间:" + dateTimeStr);4. String转LocalDateTime 我们也可以使用...
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Test { public static void main(String[] args) { String timeStr = "2022-08-15 17:09:08"; LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:...
//String -> LocalDateTime LocalDateTime stringToLocalDateTime = LocalDateTime.parse("2018-03-11 15:30:11", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println("String -> LocalDateTime: "+ stringToLocalDateTime); //String -> Date ...
1.LocalDateTime获取毫秒数 //获取秒数 Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")); //获取毫秒数 Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); 1. 2. 3. 4. 2.LocalDateTime与String互转 ...
Java爬坑--LocalDateTime与String⽇期互相转换1public static void main(String[] args) { 2 DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");3 LocalDateTime time = LocalDateTime.now();4 String localTime = df.format(time);5 LocalDateTime ldt = LocalDateTime.pars...
LocalDateTime convertTimeZoneStringToLocalDateTime(String timeZoneDateTimeStr){ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); OffsetDateTime offsetDateTime = OffsetDateTime.parse(timeZoneDateTimeStr, formatter);return offsetDateTime.toLocalDateTime(); }}这里...
1publicstaticvoidmain(String[] args) {2DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");3LocalDateTime time =LocalDateTime.now();4String localTime =df.format(time);5LocalDateTime ldt = LocalDateTime.parse("2018-01-12 17:07:05",df);6System.out.println("LocalDateTime转...