以下是我目前用到过的,后期如果遇到其他需求也会补上的,各位朋友遇到了什么转换也可以提示。
- package com.example.zyt.myutilslist.utils;
- import android.util.Log;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import java.util.Locale;
- import java.util.TimeZone;
- /**
- * Created by lzy on 2016/11/23.
- */
- public class DateUtils {
- private static String mYear; // 当前年
- private static String mMonth; // 月
- private static String mDay;//日
- /**
- * 获取当前日期几月几号
- */
- public static String getDateString() {
- 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));// 获取当前月份的日期号码
- if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth)))) {
- mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth))));
- }
- return (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "月" + (mDay.length() == 1 ? "0" + mDay : 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));// 获取当前月份的日期号码
- if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth)))) {
- mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth))));
- }
- return mYear + "-" + (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "-" + (mDay.length() == 1 ? "0" + mDay : 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) == Calendar.SUNDAY) {
- Week += "周天";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
- Week += "周一";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
- Week += "周二";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
- Week += "周三";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
- Week += "周四";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
- Week += "周五";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
- Week += "周六";
- }
- return Week;
- }
- /**
- * 获取今天往后一周的日期(几月几号)
- */
- 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);// 获取当前日份的日期号码
- if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {
- mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
- }
- String date = mMonth + "月" + mDay + "日";
- dates.add(date);
- }
- return dates;
- }
- /**
- * 获取今天往后一周的日期(哪年哪月哪日)
- */
- public static List<String> getSevendate2() {
- 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);// 获取当前日份的日期号码
- if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {
- mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
- }
- String date = mYear + "-" + (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "-" + (mDay.length() == 1 ? "0" + mDay : mDay);
- dates.add(date);
- }
- return dates;
- }
- /**
- * 获取今天往后一周的日期(几号)
- */
- public static List<String> getSevenDay() {
- 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);// 获取当前日份的日期号码
- if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {
- mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
- }
- String date = mDay;
- dates.add(date);
- }
- return dates;
- }
- /**
- * 获取今天往后一周的日期(几月几号)
- */
- public static List<String> getSevenDayMonth() {
- 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);// 获取当前日份的日期号码
- if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {
- mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
- }
- String date = (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "-" + (mDay.length() == 1 ? "0" + mDay : 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;
- }
- /**
- * 获取今天往后一周的日期(yyyy-MM-dd 年-月-日)
- */
- public static List<String> get7date() {
- List<String> dates = new ArrayList<String>();
- final Calendar c = Calendar.getInstance();
- c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
- SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
- String date = sim.format(c.getTime());
- dates.add(date);
- for (int i = 0; i < 6; i++) {
- c.add(Calendar.DAY_OF_MONTH, 1);
- date = sim.format(c.getTime());
- dates.add(date);
- }
- return dates;
- }
- /**
- * 得到当年当月的最大日期
- */
- public static int MaxDayFromDay_OF_MONTH(int year, int month) {
- Calendar time = Calendar.getInstance();
- time.clear();
- time.set(Calendar.YEAR, year);
- time.set(Calendar.MONTH, month - 1);//注意,Calendar对象默认一月为0
- int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
- return day;
- }
- public DateUtils() {}
- private static final DateUtils dateUtils = new DateUtils();
- public static DateUtils getInstance() {
- return dateUtils;
- }
- /**
- * 把当前日期转换格式为yyyy-MM-dd
- */
- public String format(Date date) {
- String format = "yyyy-MM-dd";
- SimpleDateFormat fmt = new SimpleDateFormat(format);
- return fmt.format(date);
- }
- /**
- * 把当前日期转换为指定格式
- */
- private String format(Date date, String format) {
- SimpleDateFormat fmt = new SimpleDateFormat(format);
- return fmt.format(date);
- }
- /**
- * 获得当前日期(yyyy-MM-dd)
- */
- public String getToday() {
- String result = "";
- Date date = new Date();
- result = format(date);
- return result;
- }
- /**
- * 获得明天日期(yyyy-MM-dd)
- */
- public String getTomorrow() {
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar c = Calendar.getInstance();
- c.add(Calendar.DAY_OF_MONTH, 1);
- String result = sf.format(c.getTime());
- return result;
- }
- /**
- * 获得当前时间(yyyy-MM-dd HH:mm:ss)
- */
- public String getTime() {
- String result = "";
- Date date = new Date();
- result = format(date, "yyyy-MM-dd HH:mm:ss");
- return result;
- }
- /**
- * 调此方法输入所要转换的时间输入例如("2014-06-14 16:09:00")返回时间戳
- */
- public static long dateToLong(String time) {
- SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
- Date date;
- long l = 0;
- try {
- date = sdr.parse(time);
- l = date.getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return l;
- }
- /**
- * 调此方法输入所要转换的时间、格式,返回时间戳
- */
- public static String getTimestamp(String time, String type) {
- SimpleDateFormat sdr = new SimpleDateFormat(type, Locale.CHINA);
- Date date;
- String times = null;
- try {
- date = sdr.parse(time);
- long l = date.getTime();
- String stf = String.valueOf(l);
- times = stf.substring(0, 10);//截取0-10
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return times;
- }
- /**
- * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014年06月14日16时09分00秒")
- */
- public String times(String time) {
- SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
- int i = Integer.parseInt(time);
- String times = sdr.format(new Date(i * 1000L));
- return times;
- }
- /**
- * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014-06-14")
- */
- public static String times2(String time) {
- SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd");
- int i = Integer.parseInt(time);
- String times = sdr.format(new Date(i * 1000L));
- return times;
- }
- /**
- * 调用此方法输入所要转换的时间戳例如(1402733340)输出("06月14日 周六 16:09")
- */
- public static String times3(long timeStamp) {
- SimpleDateFormat sdr = new SimpleDateFormat("MM月dd日 # HH:mm");
- return sdr.format(new Date(timeStamp * 1000L)).replaceAll("#",
- getWeek(timeStamp));
- }
- /**
- * 输入时间戳变星期,例如(1402733340)输出("周六")
- */
- public static String getWeek(long timeStamp) {
- int mydate = 0;
- String week = null;
- Calendar cd = Calendar.getInstance();
- cd.setTime(new Date(timeStamp * 1000L));
- mydate = cd.get(Calendar.DAY_OF_WEEK);
- // 获取指定日期转换成星期几
- if (mydate == 1) {
- week = "周日";
- } else if (mydate == 2) {
- week = "周一";
- } else if (mydate == 3) {
- week = "周二";
- } else if (mydate == 4) {
- week = "周三";
- } else if (mydate == 5) {
- week = "周四";
- } else if (mydate == 6) {
- week = "周五";
- } else if (mydate == 7) {
- week = "周六";
- }
- return week;
- }
- /**
- * 通过年份和月份 得到当月的日子
- */
- public static int getMonthDays(int year, int month) {
- month++;
- switch (month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- return 31;
- case 4:
- case 6:
- case 9:
- case 11:
- return 30;
- case 2:
- if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
- return 29;
- } else {
- return 28;
- }
- default:
- return -1;
- }
- }
- /**
- * 根据列名 获取周
- */
- public static String getWeekName(int column) {
- switch (column) {
- case 0:
- return "周日";
- case 1:
- return "周一";
- case 2:
- return "周二";
- case 3:
- return "周三";
- case 4:
- return "周四";
- case 5:
- return "周五";
- case 6:
- return "周六";
- default:
- return "";
- }
- }
- private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();
- /**
- * 判断是否为今天(效率比较高)
- *
- * @param day 传入的 时间 "2016-06-28 10:10:30"
- * @return true今天 false不是
- * @throws ParseException
- */
- public static boolean IsToday(String day) {
- try {
- Calendar pre = Calendar.getInstance();
- Date predate = new Date(System.currentTimeMillis());
- pre.setTime(predate);
- Calendar cal = Calendar.getInstance();
- Date date = getDateFormat().parse(day);
- cal.setTime(date);
- if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
- int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);
- if (diffDay == 0) {
- return true;
- }
- }
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return false;
- }
- public static SimpleDateFormat getDateFormat() {
- if (null == DateLocal.get()) {
- DateLocal.set(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA));
- }
- return DateLocal.get();
- }
- /**
- * 判断给定字符串时间是否为今日(效率不是很高,不过也是一种方法)
- * 传入的 时间 "2016-06-28 10:10:30"
- * true今天 false不是
- */
- public static boolean isToday(String sdate) {
- boolean b = false;
- Date time = toDate(sdate);
- Date today = new Date();
- if (time != null) {
- String nowDate = dateFormater2.get().format(today);
- String timeDate = dateFormater2.get().format(time);
- if (nowDate.equals(timeDate)) {
- b = true;
- }
- }
- return b;
- }
- /**
- * 将字符串转位日期类型
- */
- public static Date toDate(String sdate) {
- try {
- return dateFormater.get().parse(sdate);
- } catch (ParseException e) {
- return null;
- }
- }
- private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
- @Override
- protected SimpleDateFormat initialValue() {
- return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- }
- };
- private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
- @Override
- protected SimpleDateFormat initialValue() {
- return new SimpleDateFormat("yyyy-MM-dd");
- }
- };
- }
联系客服