java calendar跨年"/>
java calendar跨年
正幸盈12月31号(古天) ,隐示1月1号(今天),1月2号(后天),2014年从前了,欢迎2015年.
祝哪当ツ倒家净冤快摆!
[img]/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaW50YmlyZA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
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 calendar跨年
发布评论