java时间处理
程序员文章站
2022-05-25 14:45:49
...
时间处理 输入yyyy-MM-dd HH:mm:ss格式的时间,根据当前时间判断,分别显示今天,昨天,前天。。。
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Time { public static void main(String[] args) { String time = "2014-04-10 00:00:00"; System.out.println(Time.getAndroidTime(time)); } public static String getAndroidTime(String time) { String timeStr = ""; try{ Calendar calendar = Calendar.getInstance(); Calendar today = Calendar.getInstance(); // 今天 today.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); today.set(Calendar.MONTH, calendar.get(Calendar.MONTH)); today.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)); today.set(Calendar.HOUR_OF_DAY, 0); today.set(Calendar.MINUTE, 0); today.set(Calendar.SECOND, 0); today.clear(Calendar.MILLISECOND); Calendar yesterday = Calendar.getInstance(); // 昨天 yesterday.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); yesterday.set(Calendar.MONTH, calendar.get(Calendar.MONTH)); yesterday.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1); yesterday.set(Calendar.HOUR_OF_DAY, 0); yesterday.set(Calendar.MINUTE, 0); yesterday.set(Calendar.SECOND, 0); yesterday.clear(Calendar.MILLISECOND); Calendar beforeYesterday = Calendar.getInstance(); // 前天 beforeYesterday.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); beforeYesterday.set(Calendar.MONTH, calendar.get(Calendar.MONTH)); beforeYesterday.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 2); beforeYesterday.set(Calendar.HOUR_OF_DAY, 0); beforeYesterday.set(Calendar.MINUTE, 0); beforeYesterday.set(Calendar.SECOND, 0); beforeYesterday.clear(Calendar.MILLISECOND); int day_of_week = 0; Calendar monday = Calendar.getInstance(); //周一 day_of_week = monday.get(Calendar.DAY_OF_WEEK) - 1; if (day_of_week == 0) { day_of_week = 7; } monday.add(Calendar.DATE, -day_of_week + 1); monday.set(Calendar.HOUR_OF_DAY, 0); monday.set(Calendar.MINUTE, 0); monday.set(Calendar.SECOND, 0); monday.clear(Calendar.MILLISECOND); Calendar sunday = Calendar.getInstance(); //周日 sunday.add(Calendar.DATE, -day_of_week + 7); sunday.set(Calendar.HOUR_OF_DAY, 0); sunday.set(Calendar.MINUTE, 0); sunday.set(Calendar.SECOND, 0); sunday.clear(Calendar.MILLISECOND); Calendar year = Calendar.getInstance(); //今年的1月1日 year.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); year.set(Calendar.MONTH, 0); year.set(Calendar.DAY_OF_MONTH, 1); year.set(Calendar.HOUR_OF_DAY, 0); year.set(Calendar.MINUTE, 0); year.set(Calendar.SECOND, 0); year.clear(Calendar.MILLISECOND); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = format.parse(time); calendar.setTime(date); String hourZoneCH = getHourZoneCH(date); if(calendar.equals(today) || calendar.after(today)){ SimpleDateFormat todayFormat = new SimpleDateFormat("HH:mm"); timeStr = hourZoneCH + " " + todayFormat.format(date); }else if(calendar.after(yesterday)){ timeStr = "昨天" + hourZoneCH; }else if(calendar.after(beforeYesterday)){ timeStr = "前天" + hourZoneCH; }else if(calendar.after(monday) && calendar.before(sunday)){ int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; String dayOfWeekCH = ""; if(dayOfWeek==1){ dayOfWeekCH = "一"; }else if(dayOfWeek==2){ dayOfWeekCH = "二"; }else if(dayOfWeek==3){ dayOfWeekCH = "三"; }else if(dayOfWeek==4){ dayOfWeekCH = "四"; }else if(dayOfWeek==5){ dayOfWeekCH = "五"; }else if(dayOfWeek==6){ dayOfWeekCH = "六"; }else{ dayOfWeekCH = "日"; } timeStr = "周" + dayOfWeekCH + hourZoneCH; }else if(calendar.after(year)){ SimpleDateFormat formatYear = new SimpleDateFormat("M月d日 HH:mm"); timeStr = formatYear.format(date); }else{ SimpleDateFormat formatNotYear = new SimpleDateFormat("yyyy年M月d日 HH:mm"); timeStr = formatNotYear.format(date); } } catch (Exception e) { timeStr = "时间格式不正确"; e.printStackTrace(); } return timeStr; } public static String getHourZoneCH(Date date) { if (date == null) { return "时间为空"; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); Calendar oneHour = Calendar.getInstance(); oneHour.setTime(date); oneHour.set(Calendar.HOUR_OF_DAY, 1); oneHour.set(Calendar.MINUTE, 0); oneHour.set(Calendar.SECOND, 0); Calendar sixHour = Calendar.getInstance(); sixHour.setTime(date); sixHour.set(Calendar.HOUR_OF_DAY, 6); sixHour.set(Calendar.MINUTE, 0); sixHour.set(Calendar.SECOND, 0); Calendar elevenHour = Calendar.getInstance(); elevenHour.setTime(date); elevenHour.set(Calendar.HOUR_OF_DAY, 11); elevenHour.set(Calendar.MINUTE, 0); elevenHour.set(Calendar.SECOND, 0); Calendar thirteenHour = Calendar.getInstance(); thirteenHour.setTime(date); thirteenHour.set(Calendar.HOUR_OF_DAY, 13); thirteenHour.set(Calendar.MINUTE, 0); thirteenHour.set(Calendar.SECOND, 0); Calendar eighteenHour = Calendar.getInstance(); eighteenHour.setTime(date); eighteenHour.set(Calendar.HOUR_OF_DAY, 18); eighteenHour.set(Calendar.MINUTE, 0); eighteenHour.set(Calendar.SECOND, 0); Calendar nineteenHour = Calendar.getInstance(); nineteenHour.setTime(date); nineteenHour.set(Calendar.HOUR_OF_DAY, 19); nineteenHour.set(Calendar.MINUTE, 0); nineteenHour.set(Calendar.SECOND, 0); String hour = "傍晚"; if(calendar.before(oneHour)){ hour = "深夜"; }else if((calendar.before(sixHour) && calendar.after(oneHour)) || calendar.equals(oneHour)){ hour = "凌晨"; }else if((calendar.before(elevenHour) && calendar.after(sixHour)) || calendar.equals(sixHour)){ hour = "上午"; }else if(calendar.before(thirteenHour) && calendar.after(elevenHour) || calendar.equals(elevenHour)){ hour = "中午"; }else if(calendar.before(eighteenHour) && calendar.after(thirteenHour) || calendar.equals(thirteenHour)){ hour = "下午"; }else if(calendar.before(nineteenHour) && calendar.after(eighteenHour) || calendar.equals(eighteenHour)){ hour = "傍晚"; }else{ hour = "晚上"; } return hour; } }
上一篇: 怎么将个位十位百位.分开显示
下一篇: PS制作一个红色星球爆炸的逼真效果图