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);
}