// 日期字符串转Date
public static Date dateStr2date(String dateStr) {
dateStr = dateStr.replaceAll("-", " ").replaceAll(":", " ");
String newTimeStr = "";
String[] dateStrArray = dateStr.split(" ");
int[] timeArray = { 1, 1, 1, 0, 0, 0 };
for (int i = 0; i < dateStrArray.length; i++) {
if (i < 6) {
timeArray[i] = Integer.valueOf(dateStrArray[i]);
}
}
newTimeStr = String.format("%s-%s-%s %s:%s:%s", timeArray[0], timeArray[1], timeArray[2], timeArray[3], timeArray[4], timeArray[5]);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = sdf.parse(newTimeStr);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
// Date转字符串
public static String date2dateStr(Date date) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
// 将字符串转为时间戳
public static String dateStr2timestamp(String dateStr) {
Date d = dateStr2date(dateStr);
String re_time = "";
long l = d.getTime();
String str = String.valueOf(l);
re_time = str.substring(0, 10);
return re_time;
}
// 将时间戳转为字符串
public static String timestamp2dateStr(String timestamp) {
String re_StrTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long ltimestamp = Long.valueOf(timestamp);
re_StrTime = sdf.format(new Date(ltimestamp * 1000L));
System.out.println(re_StrTime);
return re_StrTime;
}
日期与时间戳
最新推荐文章于 2024-09-05 12:38:05 发布