Calendar获取当前日期,或前几天,或后几天的日期

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/imHanweihu/article/details/80910297

1. 获取系统当前时间

// 获取系统当前时间
Calendar now = Calendar.getInstance();
String res = sdf.format(now.getTime());
System.out.println(res); // 2018-07-04 11:50:37

2. 获取前几天的时间

// 前几天的时间
Calendar before7 = Calendar.getInstance();
before7.add(Calendar.DAY_OF_MONTH, - 7);
String res = sdf.format(before7.getTime());
System.out.println(res); // 2018-06-27 13:15:07

3. 获取后几天的时间

// 后几天的时间
Calendar after7 = Calendar.getInstance();
after7.add(Calendar.DAY_OF_MONTH, + 7);
String res = sdf.format(after7.getTime());
System.out.println(res); // 2018-07-11 11:59:51

猜你喜欢

转载自blog.csdn.net/imHanweihu/article/details/80910297
今日推荐