在項目中涉及到了一個時間差和時間段的獲取,在此記錄,僅供參考。java
package util; import java.sql.Timestamp; import java.util.TimeZone; public class Time { //此時時間若是在晚上八點後就返回到明晚八點的時間差,若是未到八點就返回到今晚八點的時間差 public static long getTimeDiff() { long current=System.currentTimeMillis();//當前時間毫秒數 long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();//今天零點零分零秒的毫秒數 long todayEighteen=zero+20*60*60*1000-1;//今天19點59分59秒的毫秒數 long tomorrowEighteen =todayEighteen + 24*60*60*1000-1 ; //明天19點59分59秒的毫秒數 if(current>todayEighteen){ Long different = tomorrowEighteen - current; return different; }else{ Long different = todayEighteen -current; return different; } } //獲取當天早上八點到晚上八點 public static Timestamp[] getTimePeriod(){ long current=System.currentTimeMillis();//當前時間毫秒數 long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();//今天零點零分零秒的毫秒數 long morning= zero+8*60*60*1000-1; //昨天20點00分01的毫秒數 long evening=zero+20*60*60*1000-1;//今天19點59分59秒的毫秒數 Timestamp [] timeList = new Timestamp[2]; timeList[0]=new Timestamp(morning); timeList[1]=new Timestamp(evening); return timeList; } }
![這裏寫圖片描述](http://static.javashuo.com/static/loading.gif)
掃碼關注做者我的技術公衆號,有關技術問題後臺回覆便可,不按期將有學習資源分享