例如我有datevalue = 1479709790;在大纪元 我想把这个大纪元的时间转换为星期一,星期一或星期二的星期几。以下将在星期日之前得到上周日。
public String getDay(long day){
this.day=day;
String dayOfWeeks;
long time=day*1000l;
int days = (int)((time/(1000*60*60*24))%7);
switch (days){
case 1:
dayOfWeeks = "Sun";
break;
case 2:
dayOfWeeks="Mon";
break;
case 3:
dayOfWeeks = "Tue";
break;
case 4:
dayOfWeeks="Wed";
break;
case 5:
dayOfWeeks = "Thru";
break;
case 6:
dayOfWeeks="Frid";
break;
case 7:
dayOfWeeks = "Sat";
break;
default:
dayOfWeeks="Sat";
}
return dayOfWeeks;
}
答案 0 :(得分:3)
假设您没有考虑时区(即假设一切都是UTC),它可能很简单:
public static DayOfWeek getDayOfWeek(long epochSecond) {
return Instant.ofEpochSecond(epochSecond).atOffset(ZoneOffset.UTC).getDayOfWeek();
}
答案 1 :(得分:0)
以上方法(ofEpochSecond(epochSecond).atOffset(ZoneOffset.UTC)
)不适用于低于26的API!
因此您可以使用以下代码
public static String getdayOfWeek(long epochinSecond) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(epochinSecond * 1000l);
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
int i = cal.get(Calendar.DAY_OF_WEEK);
String dayOfWeek;
switch (i) {
case 1:
dayOfWeek = "Sunday";
break;
case 2:
dayOfWeek = "Monday";
break;
case 3:
dayOfWeek = "Tuesady";
break;
case 4:
dayOfWeek = "Wednesday";
break;
case 5:
dayOfWeek = "Thursday";
break;
case 6:
dayOfWeek = "Friday";
break;
case 7:
dayOfWeek = "Saturday";
break;
default:
dayOfWeek = null;
}
return dayOfWeek;
}