java yyyymmddhhmm前一天,Java中常用的日期操作

直接上代码

Calendar calendar = Calendar.getInstance();

//显示当前时间

System.out.println(calendar.getTime());

//各种日期格式化

SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);

String str = format.format(calendar.getTime());

System.out.println(str);

format = new SimpleDateFormat(“yyyyMMdd”);

str = format.format(calendar.getTime());

System.out.println(str);

format = new SimpleDateFormat(“yyyy/MM/dd”);

str = format.format(calendar.getTime());

System.out.println(str);

format = new SimpleDateFormat(“yyyy/M/d”);

str = format.format(calendar.getTime());

System.out.println(str);

//获取时间戳

str = String.valueOf(calendar.getTimeInMillis());

System.out.println(“时间戳: ” + str);

//减去一天

calendar.add(Calendar.DAY_OF_YEAR, -1);

format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);

str = format.format(calendar.getTime());

System.out.println(str);

//减去一个月

calendar = Calendar.getInstance();

calendar.add(Calendar.MONTH, -1);

format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);

str = format.format(calendar.getTime());

System.out.println(str);

//减去一年

calendar = Calendar.getInstance();

calendar.add(Calendar.YEAR, -1);

format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);

str = format.format(calendar.getTime());

System.out.println(str);

//只获取日期,不包含时间

calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, 0);

calendar.set(Calendar.MINUTE, 0);

calendar.set(Calendar.SECOND, 0);

calendar.set(Calendar.MILLISECOND, 0);

format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);

str = format.format(calendar.getTime());

System.out.println(str);

//获取本月的第一天

calendar = Calendar.getInstance();

calendar.set(Calendar.DAY_OF_MONTH, 1);

format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);

str = format.format(calendar.getTime());

System.out.println(str);

//时间戳字符串转日期时间

calendar = Calendar.getInstance();

str = String.valueOf((calendar.getTimeInMillis() – 60 * 1000) ); //减去一分钟

calendar.setTimeInMillis(Long.parseLong(str));

format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);

str = format.format(calendar.getTime());

System.out.println(str);

打赏

f69872ab5631b766bfb8c62b6b7d28fa.png微信扫一扫,打赏作者吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值