安卓中獲取日期的工具類:
今天項目中用到自己獲取日期,自己寫了一個獲取時間的工具類,分享出來:
包括獲取 1 當前年月日 2 當前是周幾 3、根據日期獲取是周幾 4、獲取7天的日期 5、獲取當天往後的一週
package com.example.course;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
public class DateUtils {
private static String mYear; // 當前年
private static String mMonth; // 月
private static String mDay;
private static String mWay;
/**
* 獲取當前日期幾月幾號
*/
public static String getDateString() {
final Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 獲取當前月份
mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 獲取當前月份的日期號碼
return mMonth + "月" + mDay + "日";
}
/**
* 獲取當前年月日
*
* @return
*/
public static String StringData() {
final Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
mYear = String.valueOf(c.get(Calendar.YEAR));// 獲取當前年份
mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 獲取當前月份
mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 獲取當前月份的日期號碼
return mYear + "-" + mMonth + "-" + mDay;
}
/**
* 獲取當前是周幾
*
*/
public static String getWeekString() {
final Calendar c = Calendar.getInstance();
mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
if ("1".equals(mWay)) {
mWay = "周天";
} else if ("2".equals(mWay)) {
mWay = "週一";
} else if ("3".equals(mWay)) {
mWay = "週二";
} else if ("4".equals(mWay)) {
mWay = "週三";
} else if ("5".equals(mWay)) {
mWay = "週四";
} else if ("6".equals(mWay)) {
mWay = "週五";
} else if ("7".equals(mWay)) {
mWay = "週六";
}
return mDay;
}
/**
* 根據當前日期獲得是星期幾
*
* @return
*/
public static String getWeek(String time) {
String Week = "";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(time));
} catch (ParseException e) {
e.printStackTrace();
}
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
Week += "周天";
}
if (c.get(Calendar.DAY_OF_WEEK) == 2) {
Week += "週一";
}
if (c.get(Calendar.DAY_OF_WEEK) == 3) {
Week += "週二";
}
if (c.get(Calendar.DAY_OF_WEEK) == 4) {
Week += "週三";
}
if (c.get(Calendar.DAY_OF_WEEK) == 5) {
Week += "週四";
}
if (c.get(Calendar.DAY_OF_WEEK) == 6) {
Week += "週五";
}
if (c.get(Calendar.DAY_OF_WEEK) == 7) {
Week += "週六";
}
return Week;
}
/**
* 獲取今天往後一週的日期(年-月-日) */
public static List<String> get7date() {
List<String> dates = new ArrayList<String>();
final Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat("yyyy-MM-dd");
String date = sim.format(c.getTime());
dates.add(date);
for (int i = 0; i < 6; i++) {
c.add(java.util.Calendar.DAY_OF_MONTH, 1);
date = sim.format(c.getTime());
dates.add(date);
return dates;
}
/**
* 獲取今天往後一週的日期(幾月幾號)
*/
public static List<String> getSevendate() {
List<String > dates = new ArrayList<String>();
final Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
for (int i = 0; i < 7; i++) {
mYear = String.valueOf(c.get(Calendar.YEAR));// 獲取當前年份
mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 獲取當前月份
mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH)+i);// 獲取當前日份的日期號碼
String date =mMonth + "月" + mDay + "日";
dates.add(date);
}
return dates;
}
/**
* 獲取今天往後一週的集合
*/
public static List<String > get7week(){
String week="";
List<String > weeksList = new ArrayList<String>();
List<String> dateList = get7date();
for(String s:dateList ){
if (s.equals(StringData())) {
week="今天";
}else {
week=getWeek(s);
}
weeksList.add(week);
}
return weeksList;
}
/**
* 判斷當前日期是否爲今天
*/
public static boolean isToday(long time) {
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String param = sdf.format(date);//參數時間
String now = sdf.format(new Date());//當前時間
if (param.equals(now)) {
return true;
}
return false;
}
/**
* 獲取今天往後一天的日期(時間戳)
*時間戳 增加24小時
*/
public static long getNextDate(long time) {
long date = time + 24 * 60 * 60 * 1000;
return date;
}
/**
* 獲取今天往前一天的日期(時間戳)
*/
public static long getBeforeDate(long time) {
long date = time - 23 * 60 * 60 * 1000;
return date;
}
}