java中格林尼治时间的输出_Java中格林尼治时间和时间戳的相互转换
首先,將格林尼治時間轉(zhuǎn)換為時間戳:
/**
* 格林尼治時間轉(zhuǎn)換為時間戳
*/
public static long iso8601FormateTimeToLong(String time){
String formateTime = iso8601ToCustomerDate(time,"yyyy年M月d日 HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日 HH:mm:ss");
Date date = null;
try {
date = sdf.parse(formateTime);
} catch (ParseException e) {
MXLog.e(MXLog.APP_WARN, e);
}
return date.getTime();
}
然后是時間戳轉(zhuǎn)為格林尼治時間:
/**
* 時間戳轉(zhuǎn)成本機(jī)時區(qū)的格林尼治時間
* @param date
* @return
*/
public static String dateLongToiso8601(long date) {
DateTime dateTime = new DateTime(date);
return dateTime.toString("yyyy-MM-dd'T'HH:mm:ssZ");
}
根據(jù)時間戳判斷距離當(dāng)前時間的時間,和微信朋友圈中的時間類似,其實(shí)原理很簡單,通過傳入的時間和當(dāng)前時間作比較即可:
/**
* ?時間
* ?1)0-1min ? ? ?剛剛
* ?2)1-60min ? ? xx分鐘前,如3分鐘前
* ?3)1-24h ? ? ? ?xx小時前,如2小時前
* ?4)昨天的 ? ? ? 昨天+時+分,如,昨天 05:30
* ?5)昨天之前的 ? 月+日+時+分,如,1月3日 05:30
* ?6)非本年 ? ? ? 年+月+日+時+分,如2017年1月12日 05:30
*
* @param context
* @param time
* @return
*/
public static String formateTime3(Context context,long time){
boolean isTodayMessage = false;
Calendar todayBegin = Calendar.getInstance();
todayBegin.set(Calendar.HOUR_OF_DAY, 0);
todayBegin.set(Calendar.MINUTE, 0);
todayBegin.set(Calendar.SECOND, 0);
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
Calendar messageTime = Calendar.getInstance();
messageTime.setTime(new Date(time));
if (messageTime.before(todayEnd) && messageTime.after(todayBegin)) {
isTodayMessage = true;
}
SimpleDateFormat formatter = null;
int year = messageTime.get(Calendar.YEAR);
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTime(new Date(System.currentTimeMillis()));
int currentYear = currentCalendar.get(Calendar.YEAR);
if (isTodayMessage) {
long currentTime = System.currentTimeMillis();
long duration = currentTime - time;
if (duration < 60 * 1000 && duration >= 0) {
//60s以內(nèi)
return "剛剛";
} else if (duration >= 60 * 1000 && duration < 60 * 60 * 1000 ) {
//大于1分鐘,小于1小時
return duration /60 /1000 + "分鐘前";
} else if (duration >= 3600 * 1000 && duration < 3600 * 24 * 1000) {
//大于1小時
return duration / 3600 /1000 + "小時前";
}
}else if (isYesterday(time)){
formatter = new SimpleDateFormat("HH:mm");
return "昨天" + formatter.format(messageTime.getTime());
}else if (year != currentYear){
formatter = new SimpleDateFormat("yyyy年M月d日 HH:mm");
}else {
formatter = new SimpleDateFormat("M月d日 HH:mm");
}
if (formatter == null){
return "剛剛";
}else{
return formatter.format(messageTime.getTime());
}
}
總結(jié)
以上是生活随笔為你收集整理的java中格林尼治时间的输出_Java中格林尼治时间和时间戳的相互转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Report WebCore crash
- 下一篇: 码率控制
