码迷,mamicode.com
首页 > 编程语言 > 详细

Java获取今天/昨天/前天的某个时间点的方法

时间:2019-12-14 11:53:35      阅读:542      评论:0      收藏:0      [点我收藏+]

标签:get   row   str   div   sim   else   atd   simple   vat   

开发时需要把如“今天 12:15” "昨天 21:21" "前天 03: 02"之类的String转换成Date,所以写了个方法,有更好的写法的话欢迎留言

 1 //获取今天、昨天、前天的某个时间的方法, 如"昨天 21:21" "前天 15:15"
 2     private static Date getFormatDate(String updated) throws Exception{
 3         if (updated.substring(0, 2).equals("今天")){
 4             String partOfDate = updated.substring(3);
 5             Calendar calendar = Calendar.getInstance();
 6             calendar.setTime(new Date());
 7             calendar.set(Calendar.HOUR_OF_DAY, 0);
 8             calendar.set(Calendar.MINUTE, 0);
 9             calendar.set(Calendar.SECOND, 0);
10             SimpleDateFormat todaySdf = new SimpleDateFormat("yyyy-MM-dd");
11             String today = todaySdf.format(calendar.getTime());
12             String finalString = today + " " + partOfDate;
13             SimpleDateFormat finalSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
14             return finalSdf.parse(finalString);
15         }else if (updated.substring(0,2).equals("昨天")){
16             String partOfDate = updated.substring(3);
17             Calendar calendar = Calendar.getInstance();
18             calendar.setTime(new Date());
19             calendar.add(Calendar.DATE, -1);
20             calendar.set(Calendar.HOUR_OF_DAY, 0);
21             calendar.set(Calendar.MINUTE, 0);
22             calendar.set(Calendar.SECOND, 0);
23             SimpleDateFormat yesterdaySdf = new SimpleDateFormat("yyyy-MM-dd");
24             String yesterday = yesterdaySdf.format(calendar.getTime());
25             String finalString = yesterday + " " + partOfDate;
26             SimpleDateFormat finalSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
27             return finalSdf.parse(finalString);
28         }else if (updated.substring(0, 2).equals("前天")){
29             String partOfDate = updated.substring(3);
30             Calendar calendar = Calendar.getInstance();
31             calendar.setTime(new Date());
32             calendar.add(Calendar.DATE, -2);
33             calendar.set(Calendar.HOUR_OF_DAY, 0);
34             calendar.set(Calendar.MINUTE, 0);
35             calendar.set(Calendar.SECOND, 0);
36             SimpleDateFormat daysBeforeSdf = new SimpleDateFormat("yyyy-MM-dd");
37             String daysBefore = daysBeforeSdf.format(calendar.getTime());
38             String finalString = daysBefore + " " + partOfDate;
39             SimpleDateFormat finalSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
40             return finalSdf.parse(finalString);
41         }else {
42             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
43             return sdf.parse(updated);
44         }
45     }

 

Written on Dec. 14th, 2019

Java获取今天/昨天/前天的某个时间点的方法

标签:get   row   str   div   sim   else   atd   simple   vat   

原文地址:https://www.cnblogs.com/LittleMike/p/12038462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!