package com.warmlight.voicepacket.utils;
import android.text.TextUtils;
import com.warmlight.voicepacket.R;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by jason on 2018/5/4.
*/
public class TimeFormatUtils {
public static String getControllerTime(long time){
if(time/1000%60 <10){
return time/1000/60 + ":0" + time/1000%60;
}else{
return time/1000/60 + ":" + time/1000%60;
}
}
public static long getCurrentTimeMillis(){
long currentTime = System.currentTimeMillis();
return currentTime;
}
/**
* 將時間(單位:秒)格式化爲 X天X小時X秒的形式
*/
public static String timeForDayHours(int time) {
int currentTime = (int) (System.currentTimeMillis() / 1000);
if (currentTime < time) return "";
int dTime = currentTime - time;
int dHour = dTime / 60 / 60;
if (dHour > 24) {
return timeFormat(time);
} else {
if (dHour > 0) {
return dHour + "小時" + dTime % (60 * 60) / 60 + "分鐘前";
} else {
int m = dTime % (60 * 60) / 60;
if (m > 0) {
return m + "分鐘前";
} else {
return "剛剛";
}
}
}
}
/**
* 格式化爲yyyy-MM-dd hh:mm:ss
*
* @param time 秒
*/
public static String timeFormat(long time) {
time *= 1000;
return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(time));
}
/**
* 轉化爲時分秒
*/
public static String getHMS(long time){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
SimpleDateFormat fmat = new SimpleDateFormat("HH:mm:ss");
String curTime = fmat.format(calendar.getTime());
return curTime;
}
/**
* 格式化爲yyyy-MM-dd
*
* @param time 秒
*/
public static String timeFormatYMD(long time) {
time *= 1000;
return new SimpleDateFormat("yyyy-MM-dd").format(new Date(time));
}
/**
* 根據傳入的時間戳計算分組時間戳
* 參數單位:秒
* 返回單位:秒 分組基準
*/
public static long byGroupTimestamp(long time) {
long result = 0;
try {
result = new SimpleDateFormat("yyyy-MM-dd").parse(timeFormatYMD(time)).getTime() / 1000;
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
/*
* 將時間戳轉換爲時間
*/
public static String StampToDate(long s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
Date date = new Date(s);
res = simpleDateFormat.format(date);
return res;
}
/**
* 將字符串解析爲時間戳
*/
public static long parseString(String timeStr) {
if (TextUtils.isEmpty(timeStr)) {
return 0;
} else if (UIUtils.getString(R.string.today).equals(timeStr)) {
return byGroupTimestamp(System.currentTimeMillis() / 1000);
} else if (UIUtils.getString(R.string.yesterday).equals(timeStr)) {
return byGroupTimestamp(System.currentTimeMillis() / 1000 - 24 * 60 * 60);
} else {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(timeStr).getTime() / 1000;
} catch (ParseException e) {
e.printStackTrace();
}
}
return 0;
}
public static String timeFormatHour(long time) {
return new SimpleDateFormat("HH:mm").format(new Date(time * 1000));
}
public static String timeForDay(long time) {
long today = System.currentTimeMillis() / 1000;
long yesterday = System.currentTimeMillis() - 24 * 60 * 60;
String sTime = timeFormatYMD(time);
if (timeFormatYMD(today).equals(sTime)) {
return "今天";
} else if (timeFormatYMD(yesterday).equals(sTime)) {
return "明天";
} else {
return timeFormat(time);
}
}
// date類型轉換爲String類型
// formatType格式爲yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH時mm分ss秒
// data Date類型的時間
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
// long類型轉換爲String類型
// currentTime要轉換的long類型的時間
// formatType要轉換的string類型的時間格式
public static String longToString(long currentTime, String formatType)
throws ParseException {
Date date = longToDate(currentTime, formatType); // long類型轉成Date類型
String strTime = dateToString(date, formatType); // date類型轉成String
return strTime;
}
// string類型轉換爲date類型
// strTime要轉換的string類型的時間,formatType要轉換的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH時mm分ss秒,
// strTime的時間格式必須要與formatType的時間格式相同
public static Date stringToDate(String strTime, String formatType)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
// long轉換爲Date類型
// currentTime要轉換的long類型的時間
// formatType要轉換的時間格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH時mm分ss秒
public static Date longToDate(long currentTime, String formatType)
throws ParseException {
Date dateOld = new Date(currentTime); // 根據long類型的毫秒數生命一個date類型的時間
String sDateTime = dateToString(dateOld, formatType); // 把date類型的時間轉換爲string
Date date = stringToDate(sDateTime, formatType); // 把String類型轉換爲Date類型
return date;
}
// string類型轉換爲long類型
// strTime要轉換的String類型的時間
// formatType時間格式
// strTime的時間格式和formatType的時間格式必須相同
public static long stringToLong(String strTime, String formatType)
throws ParseException {
Date date = stringToDate(strTime, formatType); // String類型轉成date類型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date類型轉成long類型
return currentTime;
}
}
// date類型轉換爲long類型
// date要轉換的date類型的時間
public static long dateToLong(Date date) {
return date.getTime();
}
/*
* 毫秒轉化時分秒毫秒
*/
public static String timeToDayHourMilS(Long ms) {
Integer ss = 1000;
Integer mi = ss * 60;
Integer hh = mi * 60;
Integer dd = hh * 24;
Long day = ms / dd;
Long hour = (ms - day * dd) / hh;
Long minute = (ms - day * dd - hour * hh) / mi;
Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
StringBuffer sb = new StringBuffer();
if(day >= 0) {
sb.append(day+"天 ");
}
if(hour >= 0) {
sb.append(hour+"小時 ");
}
if(minute >= 0) {
sb.append(minute+"分 ");
}
if(second >= 0) {
sb.append(second+"秒");
}
// if(milliSecond > 0) {
// sb.append(milliSecond+"毫秒");
// }
return sb.toString();
}
public static String stringToString(String birth, String s) {
long time = Long.valueOf(birth);
String s1 = "";
try {
s1 = longToString(time, s);
} catch (ParseException e) {
e.printStackTrace();
}
return s1;
}
}