package com.itaoo.android.huabao.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author Stephen Zhu
*
*/
public class TimeUtil {
private static final String TAG = TimeUtil.class.getSimpleName();
/** one day millisecond count*/
public static final long ONE_DAY_MILLISECONDS = 1000*3600*24;
public static final long ONE_HOUR_MILLISECONDS = 1000*3600;
public static final long ONE_MIN_MILLISECONDS = 1000*60;
/**
* format yyyy-MM-dd mm:ss to yyyy年MM月dd日
* @param dateStr
* @return
*/
public static String formatDate(String dateStr){
String ret = dateStr.split(" ")[0];
String[] rets = ret.split("-");
ret = rets[0] + "年" + rets[1] + "月" + rets[2] + "日";
return ret;
}
/**
* get expired days
* @param dateStr
* @return -1 -> expired, 0 -> expired after today
*/
public static int getExpiredDays(String dateStr){
int ret = -1;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date;
try {
date = sdf.parse(dateStr);
Date dateNow = new Date();
long times = date.getTime() - dateNow.getTime();
if(times > 0){
ret = ((int)(times / ONE_DAY_MILLISECONDS));
}else{
ret = -1;
}
} catch (ParseException e) {
e.printStackTrace();
}
Log.d(TAG, "dateStr = " + dateStr + " days = " + ret);
return ret;
}
public static int getExpiredDays(String dateStr, String endTime){
int ret = -1;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date;
try {
date = sdf.parse(dateStr);
Date dateNow = sdf.parse(endTime);
long times = date.getTime() - dateNow.getTime();
if(times > 0){
ret = ((int)(times / ONE_DAY_MILLISECONDS));
}else{
ret = -1;
}
} catch (ParseException e) {
e.printStackTrace();
}
Log.d(TAG, "dateStr = " + dateStr + " days = " + ret);
return ret;
}
public static int getExpiredHour(String dateStr){
int ret = -1;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date;
try {
date = sdf.parse(dateStr);
Date dateNow = new Date();
long times = date.getTime() - dateNow.getTime();
if(times > 0){
ret = ((int)(times / ONE_HOUR_MILLISECONDS));
}else{
ret = -1;
}
} catch (ParseException e) {
e.printStackTrace();
}
Log.d(TAG, "dateStr = " + dateStr + " hours = " + ret);
return ret;
}
public static int getExpiredHour(String dateStr, String endTime){
int ret = -1;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date;
try {
date = sdf.parse(dateStr);
Date dateNow = sdf.parse(endTime);
long times = date.getTime() - dateNow.getTime();
if(times > 0){
ret = ((int)(times / ONE_HOUR_MILLISECONDS));
}else{
ret = -1;
}
} catch (ParseException e) {
e.printStackTrace();
}
Log.d(TAG, "dateStr = " + dateStr + " hours = " + ret);
return ret;
}
public static int getExpiredMin(String dateStr){
int ret = -1;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date;
try {
date = sdf.parse(dateStr);
Date dateNow = new Date();
long times = date.getTime() - dateNow.getTime();
if(times > 0){
ret = ((int)(times / ONE_MIN_MILLISECONDS));
}else{
ret = -1;
}
} catch (ParseException e) {
e.printStackTrace();
}
Log.d(TAG, "dateStr = " + dateStr + " mins = " + ret);
return ret;
}
public static int getExpiredMin(String dateStr, String endTime){
int ret = -1;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date;
try {
date = sdf.parse(dateStr);
Date dateNow = sdf.parse(endTime);
long times = date.getTime() - dateNow.getTime();
if(times > 0){
ret = ((int)(times / ONE_MIN_MILLISECONDS));
}else{
ret = -1;
}
} catch (ParseException e) {
e.printStackTrace();
}
Log.d(TAG, "dateStr = " + dateStr + " mins = " + ret);
return ret;
}
}
判断两个时间相差多少天多少分多少秒
最新推荐文章于 2024-07-01 02:06:56 发布