Android 之Date常見的方法使用
阿新 • • 發佈:2019-01-27
- /**
- 描述:Date型別轉化為String型別.
- @param date the dateo
- @param format 型別如 yyyy-MM-dd HH:mm:ss
*/
public static String getStringByFormat(Date date, String format) {
SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
String strDate = null;
try {
strDate = mSimpleDateFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return strDate;
}
- /**獲取當前時間
- pattern 型別 yyyy-MM-dd HH:mm:ss
- @return
*/
public static String getCurrentDate(String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date curDate = new Date(System.currentTimeMillis());// 獲取當前時間
String timestamp = formatter.format(curDate);
return timestamp;
}
- /**
- 獲取當前日期的day天后的日期
- @param pattern 格式型別
- @param day 天數
- @return
*/
public static String getNewData(String pattern, int day) {
Format format = new SimpleDateFormat(pattern);
long time = 0;
Date today = new Date();
time = (today.getTime() / 1000) + 60 * 60 * 24 * day;
Date newDate = new Date();
newDate.setTime(time * 1000);
String date = format.format(newDate);
return date;
}
/**
*
*
* @param DATE1 判斷日期是否超過當前日期 超過當前日期為true
* @return
*/
public static boolean compare_date(String DATE1) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = new Date();
if (dt1.getTime() > dt2.getTime()) {//傳入時間小於當前時間 4.7大於4.8
System.out.println("還沒到...");
return false;
} else if (dt1.getTime() <= dt2.getTime()) {
System.out.println("超過當期日期...");
return true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return false;
}
- /** 描述:比較兩個日期的大小
- old 表示之前的時間
- now 現在的時間
- @return
*/
public static String DateCompare(String old,String now){
java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Calendar c1=java.util.Calendar.getInstance();
java.util.Calendar c2=java.util.Calendar.getInstance();
try {
c1.setTime(df.parse(old));
c2.setTime(df.parse(now));
} catch (Exception e) {
System.out.println("格式不正確");
e.printStackTrace();
}
int result=c1.compareTo(c2);
if(result==0){
return "相等";
}else if(result<0){
return "小於";
}else
return "大於";
}
- /**描述
- 根據出生日期獲得年齡
- data 輸入的生日 pattern 轉換的型別 如:yyyyMM-dd
- @return
*/
- 根據出生日期獲得年齡
public static String getAge(String data,String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date curDate = new Date();//
String timestamp = formatter.format(curDate);
String brith = data.substring(0, 4);
String now = timestamp.substring(0, 4);
int age = Integer.valueOf(now) - Integer.valueOf(brith);
return age + "歲";
}
- //通過String型別的日期判斷是星期幾
/**
*
* 根據String 型別的日期判斷是星期幾??
* @param date String 型別的日期
* @param format 格式型別
* @return
*/
public static String getWeekOfDate(String date, String format) {
String[] dayofweek = new String[] { "星期日", "星期一", "星期二", "星期三", "星期四",
"星期五", "星期六" };
Calendar c = Calendar.getInstance();
try {
c.setTime((new SimpleDateFormat(format)).parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
return dayofweek[c.get(Calendar.DAY_OF_WEEK) - 1];
}
1.把String型別轉換為Date型別
// strTime要轉換的string型別的時間,formatType要轉換的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH時mm分ss秒,
// strTime的時間格式必須要與formatType的時間格式相同
public static Date stringToDate(String strTime, String formatType) {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
try {
date = formatter.parse(strTime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/**
*
* 專案中用的這個方法比較仿扣扣評論傳送時間
* @param startDate 開始日期
* @param endDate 結束日期
* @return
*/
public static String twoDateDistance(Date startDate, Date endDate) {
if (startDate == null || endDate == null) {
return null;
} else {
long timeLong = endDate.getTime() - startDate.getTime();
if (timeLong < 60 * 1000) {
return "剛剛";
} else if (timeLong < 60 * 60 * 1000) {
timeLong =timeLong/1000/60;
return timeLong+"分鐘前";
}else if(timeLong<60*60*24*1000){
timeLong = timeLong/60/60/1000;
return timeLong+"小時前";
}else if(timeLong<60*60*24*1000*7){
timeLong = timeLong/1000/ 60 / 60 / 24;
return timeLong+"天前";
}else if(timeLong<60*60*24*1000*7*4){
timeLong = timeLong/1000/ 60 / 60 / 24/7;
return timeLong+"周前";
}
else {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
return sdf.format(startDate);
}
}
}
private static String strYear;
private static String strMonth;
private static String strDay;
private static String sex;
/**
*
* @param mesg 根據生份證號獲取生日
* @return
*/
public static String SetbirthDay(String mesg){
if(mesg!=null&&mesg!=""){
strYear = mesg.substring(6, 10);
strMonth = mesg.substring(10, 12);
strDay = mesg.substring(12, 14);
}
return strYear+"-"+strMonth+"-"+strDay;
}
/**
*
* @param mesg 根據省份證號獲取性別
* @return
*/
public static String setSex(String mesg){
if(mesg!=null &&mesg!=""){
sex = mesg.substring(16, 17);
}
if(Integer.parseInt(sex)%2==0){
sex = "女";
}else{
sex ="男";
}
return sex;
}
/**
* 根據傳入的count數,來獲取count天后的日期集合
*
* @param count 表示要獲取多少天數
* @param flag 預設false
* @return
*/
public static List<String> getDateTimeStr(int count, boolean flag) {
List<String> timestr = new ArrayList<String>();
for (int i = 0; i < count; i++) {
String dateStr = getDateStr(i + 1, flag);
timestr.add(dateStr);
}
return timestr;
}
public static String getDateStr(int mon, boolean flag) {
String time = "";
if (flag) {
if (mon % 2 == 1) {
time = "01 ";
mon = mon / 2 + 1;
} else if (mon % 2 == 0) {
time = "02 ";
mon = mon / 2;
}
}
Calendar cal = Calendar.getInstance();
int _year = cal.get(Calendar.YEAR);
int _month = cal.get(Calendar.MONTH) + 1;
int _day = cal.get(Calendar.DAY_OF_MONTH) + mon;
int hour = cal.get(Calendar.HOUR_OF_DAY);
if (hour > 23) {
_day = _day + 1;
}
int lastday = getLastDay();
if (_day > lastday) {
_month = _month + 1;
_day = _day - lastday;
}
String month = "";
String day = "";
if (_month < 10) {
month = "0" + _month;
} else {
month = _month + "";
}
if (_day < 10) {
day = "0" + _day;
} else {
day = _day + "";
}
if (_month > 12) {
month = "01";
_year = _year + 1;
}
time = time + _year + "-" + month + "-" + day;
return time;
}
public static int getLastDay() {
int lastday;
Calendar d = Calendar.getInstance();
int month = d.get(Calendar.MONTH);
do {
lastday = d.get(Calendar.DAY_OF_MONTH);
d.add(Calendar.DAY_OF_MONTH, 1);
} while (d.get(Calendar.MONTH) == month);
return lastday;
}