java根据时间格式转换为字符串⼯具类⽅法总结1. 根据时间格式转换为字符串(Date date)
public static String getFormatTime(String pattern, Date date) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.TimeZone("GMT+08:00"));
return sdf.format(date ==null ?new Date() : date);
}
调⽤举例:String strTime = FormatTime("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.TimeZone("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 = FormatTimeToLong("yyyy-MM-dd HH:mm:ss", time);
public static String getFormatTime(String pattern,long date) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.TimeZone("GMT+08:00"));
Date date2 =new Date();
date2.setTime(date);
return sdf.format(date2);
}
调⽤举例:String strTime = FormatTime("yyyy-MM-dd HH:mm:ss", millisecond );
3.根据时间格式转换为字符串(String date)
public static String getFormatTime(String pattern, String date) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.TimeZone("GMT+08:00"));
try {
return sdf.format(sdf.parse(date).getTime());
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
调⽤举例:String strTime = FormatTime("yyyy-MM-dd", "String时间字符串");
4.根据格式互转
public static String getFormatTimeFromPatternToPattern(String rawPattern, String pattern, String date) { SimpleDateFormat sdf =new SimpleDateFormat(rawPattern);
sdf.TimeZone("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.TimeZone("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 FormatTime("yyy-MM-dd HH:mm:ss",new FormatTimeToLong("yyy-MM-dd HH:mm:ss", "String时间字符串")))
6.根据时间格式转换为时间戳(返回Date类型)
public static Date getFormatTimeToDate(String pattern, String time) {
SimpleDateFormat sdf =new SimpleDateFormat(pattern);
sdf.TimeZone("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.TimeZone("GMT+08:00"));
return sdf.format(date ==null ?new Date() : date);
}
举例调⽤:Calendar calendar = Instance();
Date date2 =new Date(getFormatTimeToLong("yyyy-MM-dd", "String时间字符串"));
calendar.setTime(date2);
calendar.add(Calendar.DATE, +1);
String strTime= FormatTimeNoZone("yyyy-MM-dd", Time());
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天前)
string转date的方法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 = Instance();
Calendar c = Instance();
c.setTimeInMillis(timestamp);
if (c.get(Calendar.YEAR) == (Calendar.YEAR)
&& c.get(Calendar.MONTH) == (Calendar.MONTH)
&& c.get(Calendar.DATE) == (Calendar.DATE)) {//
(Calendar.HOUR_OF_DAY) - c.get(Calendar.HOUR_OF_DAY) +"⼩时前"; }
if (c.get(Calendar.YEAR) == (Calendar.YEAR)
&& c.get(Calendar.MONTH) == (Calendar.MONTH)
&& c.get(Calendar.DATE) == (Calendar.DATE) -1) {
SimpleDateFormat sdf =new SimpleDateFormat("昨天 HH:mm");
return sdf.Time());
}else if (c.get(Calendar.YEAR) == (Calendar.YEAR)
&& c.get(Calendar.MONTH) == (Calendar.MONTH)) {
(Calendar.DAY_OF_MONTH) - c.get(Calendar.DAY_OF_MONTH) +"天前";
}else if (c.get(Calendar.YEAR) == (Calendar.YEAR)) {
(Calendar.MONTH) - c.get(Calendar.MONTH) +"⽉前";
}else {
return new SimpleDateFormat("yyyy年M⽉d⽇").Time());
}
}
}catch (Exception e) {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。