Java:时间戳获取和日期时间格式化
【摘要】 package com.example.demo;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatte...
package com.example.demo;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
public class TimeDemo { public static void main(String[] args) { // 旧API System.out.println(System.currentTimeMillis()); // 1594801771810 Date date = new Date(); System.out.println(date.getTime()); // 1594802296250 Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTimeInMillis()); // 1594802296258 // 新API Instant instant = Instant.now(); System.out.println(instant.getEpochSecond()); // 1594802296 System.out.println(instant.toEpochMilli()); // 1594802296321 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime.toEpochSecond(ZoneOffset.of("+8"))); // 1594802868 ZonedDateTime zonedDateTime = ZonedDateTime.now(); System.out.println(zonedDateTime.toEpochSecond()); // 1594803196 // 格式化 替换 SimpleDateFormat DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(formatter.format(localDateTime)); // 2020-07-15 16:44:26 System.out.println(LocalDateTime.parse("2020-07-15 16:43:47", formatter)); // 2020-07-15T16:43:47 }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/107365201
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)