Android 一些擷取時間和日期,時間日期格式化轉換的常用工具類
format:自定義的格式化規則
public class DataUtils {
private static final String TAG = "DataUtils";
/**
* 擷取自定義格式化的今日日期
*/
public static String getTodayData(String format) {
return new java.text.SimpleDateFormat(format, Locale.CHINA).format(new Date(System.currentTimeMillis()));
}
/**
* 擷取自定義格式化的昨天日期
*/
public static String getDayAgoData(String format) {
return new java.text.SimpleDateFormat(format, Locale.CHINA).format(new Date(System.currentTimeMillis() - getOneDayTimes()));
}
/**
* 擷取自定義格式化的明天日期
*/
public static String getDayLaterData(String format) {
return new java.text.SimpleDateFormat(format, Locale.CHINA).format(new Date(System.currentTimeMillis() + getOneDayTimes()));
}
/**
* 擷取前n天的自定義格式化的日期
*/
public static String getLeftDayData(String format, int count) {
return new java.text.SimpleDateFormat(format, Locale.CHINA).format(new Date(System.currentTimeMillis() - getOneDayTimes() * count));
}
/**
* 擷取上n周的周一和周日日期
*
* @param mondayFormat 周一的格式化日期
* @param sundayFormat 周日的格式化日期
* @param count 上n周的日期
* @return 周一格式化日期 + "至" + 周日格式化日期
*/
public static String getLeftWeekData(String mondayFormat, String sundayFormat, int count) {
// 今天是周幾
int week = getWeek();
// 周一
String monday = new SimpleDateFormat(mondayFormat, Locale.CHINA).format(new Date(System.currentTimeMillis() - getOneDayTimes() * (week - 1) - getOneDayTimes() * 7 * count));
// 周日
String sunday = new SimpleDateFormat(sundayFormat, Locale.CHINA).format(new Date(System.currentTimeMillis() + getOneDayTimes() * (7 - week) - getOneDayTimes() * 7 * count));
return monday + "至" + sunday;
}
/**
* 擷取前n個月要顯示的日期
*
* @param format 格式化規則
* @param count 前n個月,-為前n個月, +為後n個月
* @return 要顯示的日期
*/
public static String getLeftMouthData(String format, int count) {
//擷取目前月第一天:
Calendar calendar = Calendar.getInstance();//擷取目前日期
calendar.add(Calendar.MONTH, -count); // 減n個月
return new SimpleDateFormat(format, Locale.CHINA).format(calendar.getTime());
}
/**
* 時間字元串轉為時間戳
*
* @param dateString 已經格式化好的日期字元串
* @param pattern 格式化規則
* @return 時間戳
*/
public static long getStringToDate(String dateString, String pattern) {
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
Date date = new Date();
try {
date = dateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime();
}
/**
* 将毫秒值轉為時分秒字元串
*/
public static String longTimeToString(long time) {
// 秒
long second = time / 1000;
// if (second < 60) return second + "秒";
// 分鐘
long minute = second / 60;
if (minute < 60) return "0時" + minute + "分";
int hour = (int) minute / 60;
int minutes = (int) minute % 60;
return hour + "時" + minutes + "分";
}
/**
* 将具體的時間戳轉為格式化的時間
*/
public static String longTimeToString(String format, long time) {
return new SimpleDateFormat(format, Locale.CHINA).format(new Date(time));
}
/**
* 判斷目前日期是星期幾
*/
public static int getWeek() {
int Week = 0;
final Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
if ("1".equals(mWay)) {
Week = 7;
} else if ("2".equals(mWay)) {
Week = 1;
} else if ("3".equals(mWay)) {
Week = 2;
} else if ("4".equals(mWay)) {
Week = 3;
} else if ("5".equals(mWay)) {
Week = 4;
} else if ("6".equals(mWay)) {
Week = 5;
} else if ("7".equals(mWay)) {
Week = 6;
}
return Week;
}
/**
* 根據date算出周幾
*/
public static int getWeekOfDate(Date date) {
int[] weekOfDays = {7, 6, 5, 4, 3, 2, 1};
Calendar calendar = Calendar.getInstance();
if (date != null) {
calendar.setTime(date);
}
int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekOfDays[w];
}
/**
* 根據時間戳算出周幾
*/
public static int getWeekOfDate(long longTime) {
int[] weekOfDays = {7, 1, 2, 3, 4, 5, 6};
Calendar calendar = Calendar.getInstance();
if (longTime != 0) {
calendar.setTime(new Date(longTime));
}
int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekOfDays[w];
}
/**
* 擷取一周的時間戳,周一和周日
*
* @param count 前n周,n為前n周 目前周:n = 0
* @return 目前選擇周的時間戳
*/
public static long[] getFirstAndLastDayOfWeek(int count) {
long current = System.currentTimeMillis();
// 如果要計算目前時間到當日零點的偏移,應該使用
// long aa = (current + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 *
// 24);
// 當天0點
long start = current - (current + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24);
// 當天23點59分
long end = start + 24 * 60 * 60 * 1000 - 1000;
int week = getWeek();
// 周一時間戳 : 周一時間戳 - 7天的時間戳
long mondayTime = (start - getOneDayTimes() * (week - 1)) - (count * 7) * getOneDayTimes();
// 上n周日時間戳
long sundayTime = (end + getOneDayTimes() * (7 - week)) - (count * 7) * getOneDayTimes();
System.out.println("cal.getTime().getTime()-->" + "");
return new long[]{mondayTime, sundayTime};
}
/**
* 擷取一個月的時間戳,第一天和最後一天
*
* @param count 前n個月,n為前n個月 目前月:n = 0;
* @return 目前選擇月的時間戳
*/
public static long[] getFirstAndLastDayOfMouth(int count) {
Calendar cal_1 = Calendar.getInstance();//擷取目前日期
cal_1.add(Calendar.MONTH, count); // 減n個月
cal_1.set(Calendar.DAY_OF_MONTH, 1);// 設定目前日為1号
long firstTime = cal_1.getTime().getTime();
//擷取目前月最後一天
Calendar ca = Calendar.getInstance(); //擷取目前日期
ca.add(Calendar.MONTH, count);
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
long lastTime = ca.getTime().getTime();
// 傳回第一天和最後一天的時間戳
LogUtils.e(TAG, "mouth of first day -->" + DataUtils.longTimeToString("yyyy年MM月dd日 HH:mm:ss", firstTime));
long start = firstTime - (firstTime + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24);
LogUtils.e(TAG, "mouth of start day -->" + DataUtils.longTimeToString("yyyy年MM月dd日 HH:mm:ss", start));
LogUtils.e(TAG, "mouth of last day -->" + DataUtils.longTimeToString("yyyy年MM月dd日 HH:mm:ss", lastTime));
long end = lastTime - (lastTime + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24) + 24 * 60 * 60 * 1000 - 1000;
LogUtils.e(TAG, "mouth of end day -->" + DataUtils.longTimeToString("yyyy年MM月dd日 HH:mm:ss", end));
return new long[]{start, end};
}
/**
* 擷取一天的毫秒值
*/
public static long getOneDayTimes() {
return 1000l * 60l * 60l * 24l;
}
/**
* 擷取一月的毫秒值(30天)
*/
public static long getOneMonthTimes() {
return 1000l * 60l * 60l * 24l * 30l;
}
/**
* 今天的零點值
*/
public static long getZeroDayTime() {
long current = System.currentTimeMillis();
// return current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
return current - (current + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24);
}
/**
* 今天的23點時間戳
*/
public static long getTwelveTime() {
long current = System.currentTimeMillis();
long zero = current - (current + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24);
return zero + 24 * 60 * 60 * 1000 - 1000;//今天23點59分59秒的毫秒數;
}
/**
* 擷取格式化的包含目前日期所在周的日期
*/
public static String[] getWeekFormatDate(String format) {
String[] weekDays = new String[7];
long[] firstAndLastDayOfWeek = getFirstAndLastDayOfWeek(0);
long sunday = firstAndLastDayOfWeek[1];
for (int i = 0; i < 7; i++) {
long currentTime = sunday - getOneDayTimes() * i;
String s = longTimeToString(format, currentTime);
weekDays[6 - i] = s;
}
return weekDays;
}
/**
* 得到指定月的天數
*/
public static int getMonthAllDay(int count) {
Calendar a = Calendar.getInstance();
a.add(Calendar.MONTH, count);
a.set(Calendar.DATE, 1);//把日期設定為當月第一天
a.roll(Calendar.DATE, -1);//日期復原一天,也就是最後一天
return a.get(Calendar.DATE);
}
/**
* 擷取當月中,距今天的天數
* 今天為1号,則為1
*/
public static int getMonthDayFromFirstToToday() {
return Integer.valueOf(longTimeToString("dd", System.currentTimeMillis()));
}
public static int getYear(){
return Calendar.getInstance().get(Calendar.YEAR);
}