首页 > 代码库 > java:今天明天后天的状态获取,跨年啦
java:今天明天后天的状态获取,跨年啦
正好在12月31号(今天) ,显示1月1号(明天),1月2号(后天),2014年过去了,迎接2015年.
祝大家元旦快乐!
import java.io.File; import java.util.Calendar; import java.util.TimeZone; public class Test { public static void main(String[] args) { Test test =new Test(); test.printDay(-1); test.printDay(0); test.printDay(1); test.printDay(2); test.printDay(3); } private void printDay(int tempSingle){ Calendar calendar =Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, tempSingle); setTime(calendar.getTimeInMillis()); System.out.println(getFormatTimeNear3Day()); } private long timeMills; public void setTime(long timeMills){ this.timeMills = timeMills; } public long getTime() { return timeMills; } public String getFormatTime(){ return new DateTime(getTime()).getStringDateWeek(DateTime.FORMAT_MD_HOUR_MINUTE_WEEK); } public String getFormatTimeOnlyWeek(){ return new DateTime(getTime()).getStringDateWeek(DateTime.FORMAT_MD_HOUR_ONLY_WEEK); } public String getFormatTimeNear3Day(){ String timeText = ""; Calendar calendar= Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); int cyear = calendar.get(Calendar.YEAR); int ctoday = calendar.get(Calendar.DAY_OF_YEAR); int ctotalDay = calendar.getMaximum(Calendar.DAY_OF_YEAR); calendar.setTimeInMillis(getTime()); int year = calendar.get(Calendar.YEAR); int day = calendar.get(Calendar.DAY_OF_YEAR); switch(year - cyear){ case -1: default: timeText = getNear3Day(2,8); break; case 0: timeText = getNear3Day(day,ctoday); break; case 1: int vday = day+(ctotalDay-ctoday)-1; timeText = getNear3Day(day+vday,day); break; } return timeText; } public String getNear3Day(int day,int ctoday){ String timeText= ""; switch (day - ctoday) { case 0: timeText= "今天"; timeText += getFormatTimeOnlyWeek(); break; case 1: timeText = "明天"; timeText += getFormatTimeOnlyWeek(); break; case 2: timeText = "后天"; timeText += getFormatTimeOnlyWeek(); break; default: timeText = getFormatTime(); break; } return timeText; } }
java:今天明天后天的状态获取,跨年啦
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。