1. 根据时间格式转换为字符串(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);
}
调用举例:String strTime = StringUtil.getFormatTime("yyyy-MM-dd",new Date());
2.根据时间格式转换为字符串(long date)
/**
* 根据时间格式转换为时间戳
* @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;
}
调用举例: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);
}
调用举例:String strTime = StringUtil.getFormatTime("yyyy-MM-dd HH:mm:ss", millisecond );
3.根据时间格式转换为字符串(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 "";
}
调用举例:String strTime = StringUtil.getFormatTime("yyyy-MM-dd", "String时间字符串");
4.根据格式互转
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;
}
举例调用:String strTime=getFormatTimeFromPatternToPattern("yyyy年MM月dd日","yyyy-MM-dd", date)
5.根据时间格式转换为时间戳(返回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;
}
举例调用:String strTime=StringUtil.getFormatTime("yyy-MM-dd HH:mm:ss",new Date(StringUtil.getFormatTimeToLong("yyy-MM-dd HH:mm:ss", "String时间字符串")))
6.根据时间格式转换为时间戳(返回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();
}
}
举例调用:Date date3 =getFormatTimeToDate("HH:mm", "String时间字符串");
6.根据时间格式转换为字符串(返回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);
}
举例调用:Calendar calendar = Calendar.getInstance();
Date date2 =new Date(getFormatTimeToLong("yyyy-MM-dd", "String时间字符串"));
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.处理时间(毫秒格式 转换为n天前)
public static String fixTime(long timestamp) {
LogUtil.e("时间",getFormatTime("yyyy-MM-dd", timestamp));
try {
if (timestamp ==0) {//不识别
return "";
}
if (System.currentTimeMillis() - timestamp <1 *60 *1000) {//小于一分钟
return "刚刚";
}else if (System.currentTimeMillis() - timestamp <60 *60 *1000) {//小于一小时
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) +"小时前";
}
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 "";
}
}
调用举例:String strTime=fixTime(StringUtil.getFormatTimeToLong("yyyy-MM-dd HH:mm:ss", freeBean.getConTime()))