最近开发过程中,后台返回的时间戳是这样的:
"created_time": "2016-06-16T15:02:32" 中间有个T,在以前的开发过程中是返回一个11位的时间戳,也就是秒,这样我转换是这样的:
if (TextUtils.isEmpty(bean.getCreate_time()) ) {
holder.shop_remark_date.setText("未知时间");
} else {
Long timestamp = Long.parseLong(bean.getCreate_time()) * 1000;
String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestamp));
holder.shop_remark_date.setText(date);
}
现在多了个T,转换成正常的代码如下:
try {
String ts =bean.getCreated_time();
ts = ts.replace("Z","UTC");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dt = sdf.parse(ts);
TimeZone tz = sdf.getTimeZone();
Calendar c = sdf.getCalendar();
String aa=getString(c);
holder.tv_time.setText(aa);
LogUtil.i("aa=="+aa);
} catch (ParseException pe) {
pe.printStackTrace();
}
private static String getString(Calendar c) {
StringBuffer result = new StringBuffer();
result.append(c.get(Calendar.YEAR));
result.append("-");
result.append((c.get(Calendar.MONTH) + 1));
result.append("-");
result.append(c.get(Calendar.DAY_OF_MONTH));
result.append(" ");
result.append(c.get(Calendar.HOUR_OF_DAY));
result.append(":");
result.append(c.get(Calendar.MINUTE));
result.append(":");
result.append(c.get(Calendar.SECOND));
return result.toString();
}
打印aa==2016-06-16 15:02:32 就ok了.
在分享个时间转换工具类:
package com.longway.wifiwork_android.util;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import Android.text.TextUtils;
import android.text.format.Time;
import android.util.Log;
public class TimeUtil {
public static boolean isExpired(String issued, long validate) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
Date d = dateFormat.parse(issued);
long current = System.currentTimeMillis();
return current - d.getTime() > validate * 1000;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
public static String fromatTTime(String time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss");
try {
Date d = simpleDateFormat.parse(time);
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return simpleDateFormat.format(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public static long fromatTTTime(String time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss");
try {
Date d = simpleDateFormat.parse(time);
return d.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public static String fromatTSTime(String time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS");
try {
Date d = simpleDateFormat.parse(time);
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public static String fromatTime(String time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS");
try {
Date d = simpleDateFormat.parse(time);
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return simpleDateFormat.format(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public static String fromatTSTime2MM_HH_MM(String time) {
if(TextUtils.isEmpty(time)){
return "";
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss");
try {
Date d = simpleDateFormat.parse(time);
simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
return simpleDateFormat.format(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public static String fromatYTime(String time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss");
try {
Date d = simpleDateFormat.parse(time);
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public static String fromatYDate(String time) {
if (TextUtils.isEmpty(time) || "null".equals(time)) {
return "";
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
try {
Date d = simpleDateFormat.parse(time.split(" ")[0].substring(5));
simpleDateFormat = new SimpleDateFormat("MM-dd");
return simpleDateFormat.format(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public static Date StrToDate(String str) {
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static int[] getCurrentFormatTime() {
int[] date = new int[3];
Calendar calendar = Calendar.getInstance();
int y = calendar.get(Calendar.YEAR);
int m = calendar.get(Calendar.MONTH);
int d = calendar.get(Calendar.DAY_OF_MONTH);
date[0] = y;
date[1] = m;
date[2] = d;
return date;
}
public static int[] getFormatTime(String time, String regex) {
String[] s = time.split(regex);
int d = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]) - 1;
int y = Integer.parseInt(s[2]);
return new int[] { y, m, d };
}
public static String getHMFormtTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm",
Locale.getDefault());
return dateFormat.format(System.currentTimeMillis());
}
public static String getCurrentTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
return dateFormat.format(System.currentTimeMillis());
}
public static long getLongTime(String timestr) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm",
Locale.getDefault());
try {
return dateFormat.parse(timestr).getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
public static String getCurrentTimeYMDHM() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm",
Locale.getDefault());
return dateFormat.format(System.currentTimeMillis());
}
public static String getCurrentTimeYMD() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
return dateFormat.format(System.currentTimeMillis());
}
public static String getYMDHM() {
Time time = new Time();
time.setToNow();
return time.toString();
}
public static String getCurrentTimeYMDHM(long time) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm",
Locale.getDefault());
return dateFormat.format(time);
}
public static String[] calcuateSchedule(String startTime, String endTime) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String data[] = new String[2];
try {
long start = dateFormat.parse(startTime).getTime();
long end = dateFormat.parse(endTime).getTime();
long div = (end - start);
// if (div >= 0 && div < 1000) {
// data[0] = "毫秒";
// data[1] = div + "";
// return data;
// }
// if (div >= 1000 && div < 1000 * 60) {
// data[0] = "秒";
// data[1] = div / 1000 + "";
// return data;
// }
// if (div >= 1000 * 60 && div < 1000 * 60 * 60) {
// data[0] = "分钟";
// data[1] = div / 1000 / 60 + "";
// return data;
// }
//
// if (div >= 1000 * 60 * 60 && div < 1000 * 60 * 60 * 24) {
// data[0] = "小时";
// data[1] = div / 1000 / 60 / 60 + "";
// return data;
// }
//
// if (div >= 1000 * 60 * 60 * 24 && div < 1000 * 60 * 60 * 24 *
// 365L) {
data[0] = "天";
DecimalFormat decimalFormat = new DecimalFormat("0.0");
data[1] = decimalFormat.format(div * 1.0 / 1000 / 60 / 60 / 24);
return data;
// }
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}