小墨の博客

梦想需要付诸行动,否则只能是梦

java时间操作,时间戳转calender


1、获取时间前几天或者后几天

Calendar now=Calendar.getInstance();
now.add(Calendar.DATE,10);

2、Calendar时间戳转换

Calendar now=Calendar.getInstance();
System.out.println(now.getTimeInMillis());
Calendar now=Calendar.getInstance();
now.setTimeInMillis(1559188481978L);

3、Calendar格式化时间

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar now = Calendar.getInstance();
System.out.println(df.format(now.getTime()));

4、String 转化Calendar

String str="2019-1-1";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

5、String 转化Date

String str="2019-1-1";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);

6、Timestamp转String

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp ts = new Timestamp(System.currentTimeMillis());
String xxx=sdf.format(ts);

7、String转Timestamp

Timestamp ts = Timestamp.valueOf("2019-01-01 12:00:00");

8、Timestamp转LocalDateTime

Timestamp ts=Timestamp.from(Instant.now());
LocalDateTime ldt=ts.toLocalDateTime();

9、LocalDateTime转Timestamp

LocalDateTime ldt = LocalDateTime.now();
Timestamp ts = Timestamp.valueOf(ldt);

10、LocalDateTime转String

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
String localTime = df.format(time);

11、String转LocalDateTime

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse("2019-08-23 12:00:00", df);

 

张小弟之家

本文链接:
文章标题:

本站文章除注明转载/出处外,均为原创,若要转载请务必注明出处。转载后请将转载链接通过邮件告知我站,谢谢合作。本站邮箱:admin@only4.work

尊重他人劳动成果,共创和谐网络环境。点击版权声明查看本站相关条款。

    发表评论:

    搜索
    本文二维码
    标签列表
    站点信息
    • 文章总数:508
    • 页面总数:20
    • 分类总数:92
    • 标签总数:208
    • 评论总数:61
    • 浏览总数:225323

    | | |
    | |  Z-Blog PHP