首页 > 代码库 > 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:今天明天后天的状态获取,跨年啦