1. 根據(jù)時(shí)間格式轉(zhuǎn)換為字符串(Date date)
public static String getFormatTime(String pattern, Date date) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
return sdf.format(date ==null ?new Date() : date);
}
調(diào)用舉例:String strTime = StringUtil.getFormatTime("yyyy-MM-dd",new Date());
2.根據(jù)時(shí)間格式轉(zhuǎn)換為字符串(long date)
/**
* 根據(jù)時(shí)間格式轉(zhuǎn)換為時(shí)間戳
* @param pattern
* @param time
* @return long
*/
public static long getFormatTimeToLong(String pattern, String time) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
try {
if (!TextUtils.isEmpty(time)) {
return sdf.parse(time).getTime();
}
}catch (ParseException e) {
// TODO Auto-generated catch block
? ? ? ? e.printStackTrace();
}
return 0;
}
調(diào)用舉例:long millisecond = StringUtil.getFormatTimeToLong("yyyy-MM-dd HH:mm:ss", time);
public static String getFormatTime(String pattern,long date) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
Date date2 =new Date();
date2.setTime(date);
return sdf.format(date2);
}
調(diào)用舉例:String strTime = StringUtil.getFormatTime("yyyy-MM-dd HH:mm:ss", millisecond );
3.根據(jù)時(shí)間格式轉(zhuǎn)換為字符串(String date)
public static String getFormatTime(String pattern, String date) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
try {
return sdf.format(sdf.parse(date).getTime());
}catch (ParseException e) {
// TODO Auto-generated catch block
? ? ? ? e.printStackTrace();
}
return "";
}
調(diào)用舉例:String strTime = StringUtil.getFormatTime("yyyy-MM-dd", "String時(shí)間字符串");
4.根據(jù)格式互轉(zhuǎn)
public static String getFormatTimeFromPatternToPattern(String rawPattern, String pattern, String date) {
SimpleDateFormat sdf =new SimpleDateFormat(rawPattern);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
SimpleDateFormat newSdf =new SimpleDateFormat(pattern);
try {
return newSdf.format(sdf.parse(date));
}catch (ParseException e) {
// TODO Auto-generated catch block
? ? ? ? e.printStackTrace();
}
return date;
}
舉例調(diào)用:String strTime=getFormatTimeFromPatternToPattern("yyyy年MM月dd日","yyyy-MM-dd", date)
5.根據(jù)時(shí)間格式轉(zhuǎn)換為時(shí)間戳(返回long類型)
public static long getFormatTimeToLong(String pattern, String time) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
try {
if (!TextUtils.isEmpty(time)) {
return sdf.parse(time).getTime();
}
}catch (ParseException e) {
// TODO Auto-generated catch block
? ? ? ? e.printStackTrace();
}
return 0;
}
舉例調(diào)用:String strTime=StringUtil.getFormatTime("yyy-MM-dd HH:mm:ss",new Date(StringUtil.getFormatTimeToLong("yyy-MM-dd HH:mm:ss", "String時(shí)間字符串")))
6.根據(jù)時(shí)間格式轉(zhuǎn)換為時(shí)間戳(返回Date類型)
public static Date getFormatTimeToDate(String pattern, String time) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
try {
Date newTime = sdf.parse(time);
return newTime;
}catch (ParseException e) {
return new Date();
}
}
舉例調(diào)用:Date date3 =getFormatTimeToDate("HH:mm", "String時(shí)間字符串");
6.根據(jù)時(shí)間格式轉(zhuǎn)換為字符串(返回String類型的)
public static String getFormatTimeNoZone(String pattern, Date date) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
return sdf.format(date ==null ?new Date() : date);
}
舉例調(diào)用:Calendar calendar = Calendar.getInstance();
Date date2 =new Date(getFormatTimeToLong("yyyy-MM-dd", "String時(shí)間字符串"));
calendar.setTime(date2);
calendar.add(Calendar.DATE, +1);
String strTime= StringUtil.getFormatTimeNoZone("yyyy-MM-dd", calendar.getTime());
7.獲取中文月份
public static String[]mBigNums =new String[]{"一","二","三","四","五","六","七","八","九","十","十一","十二"};
public static String getBigMonth(int month) {
if (mBigNums.length > month -1) {
return mBigNums[month -1];
}
return "";
}
8.處理時(shí)間(毫秒格式 轉(zhuǎn)換為n天前)
public static String fixTime(long timestamp) {
LogUtil.e("時(shí)間",getFormatTime("yyyy-MM-dd", timestamp));
try {
if (timestamp ==0) {//不識(shí)別
? ? ? ? ? ? return "";
}
if (System.currentTimeMillis() - timestamp <1 *60 *1000) {//小于一分鐘
? ? ? ? ? ? return "剛剛";
}else if (System.currentTimeMillis() - timestamp <60 *60 *1000) {//小于一小時(shí)
? ? ? ? ? ? return ((System.currentTimeMillis() - timestamp) /1000 /60) +"分鐘前";
}else {
Calendar now = Calendar.getInstance();
Calendar c = Calendar.getInstance();
c.setTimeInMillis(timestamp);
if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)
&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
&& c.get(Calendar.DATE) == now.get(Calendar.DATE)) {//
? ? ? ? ? ? ? ? return now.get(Calendar.HOUR_OF_DAY) - c.get(Calendar.HOUR_OF_DAY) +"小時(shí)前";
}
if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)
&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
&& c.get(Calendar.DATE) == now.get(Calendar.DATE) -1) {
SimpleDateFormat sdf =new SimpleDateFormat("昨天 HH:mm");
return sdf.format(c.getTime());
}else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)
&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)) {
return now.get(Calendar.DAY_OF_MONTH) - c.get(Calendar.DAY_OF_MONTH) +"天前";
}else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
return now.get(Calendar.MONTH) - c.get(Calendar.MONTH) +"月前";
}else {
return new SimpleDateFormat("yyyy年M月d日").format(c.getTime());
}
}
}catch (Exception e) {
e.printStackTrace();
return "";
}
}
調(diào)用舉例:String strTime=fixTime(StringUtil.getFormatTimeToLong("yyyy-MM-dd HH:mm:ss", freeBean.getConTime()))