package com.fengzhi.datetime; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import android.annotation.SuppressLint; @SuppressLint("SimpleDateFormat") public class DateUtils { /** * * @Title: getYear * @Description: TODO(获取当前日期:年) * @author fyf * @return */ public static int getYear() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.YEAR); } /** * * @Title: getMonthOfYear * @Description: TODO(获取当前日期:月) * @author fyf * @return */ public static int getMonthOfYear() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.MONTH); } /** * * @Title: getDayOfMonth * @Description: TODO(获取当前日期:日) * @author fyf * @return */ public static int getDayOfMonth() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.DAY_OF_MONTH); } /** * * @Title: getHourOfDay * @Description: TODO(获取当前时间:时) * @author fyf * @return */ public static int getHourOfDay() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.HOUR_OF_DAY); } /** * * @Title: getMinute * @Description: TODO(获取当前时间:分) * @author fyf * @return */ public static int getMinute() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.MINUTE); } /** * * @Title: getSecond * @Description: TODO(获取当前时间:秒) * @author fyf * @return */ public static int getSecond() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.SECOND); } /** * * @Title: getMilliSecond * @Description: TODO(获取当前时间:毫秒) * @author fyf * @return */ public static int getMilliSecond() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.MILLISECOND); } /** * * @Title: getDayOfYear * @Description: TODO(获取当前日期属于当年的第几天) * @author fyf * @return */ public static int getDayOfYear() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.DAY_OF_YEAR); } /** * * @Title: getDayOfYearFromString * @Description: TODO(获取该日期是当年的第几天) * @author fyf * @param date * @return */ public static int getDayOfYearFromString(String date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(getYyyyMMddFromString(date)); return calendar.get(Calendar.DAY_OF_YEAR); } /** * * @Title: getDayOfWeekFromString * @Description: TODO(获取该日期是星期几) * @author fyf * @param date * @return */ public static String getDayOfWeekFromString(String date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(getYyyyMMddFromString(date)); switch (calendar.get(Calendar.DAY_OF_WEEK)) { case Calendar.SUNDAY: return "星期日"; case Calendar.MONDAY: return "星期一"; case Calendar.TUESDAY: return "星期二"; case Calendar.WEDNESDAY: return "星期三"; case Calendar.THURSDAY: return "星期四"; case Calendar.FRIDAY: return "星期五"; case Calendar.SATURDAY: return "星期六"; } return ""; } /** * * @Title: getDayOfWeek * @Description: TODO(获取当前日期是本周星期几) * @author fyf * @return */ public static String getDayOfWeek() { Calendar calendar = Calendar.getInstance(); switch (calendar.get(Calendar.DAY_OF_WEEK)) { case Calendar.SUNDAY: return "星期日"; case Calendar.MONDAY: return "星期一"; case Calendar.TUESDAY: return "星期二"; case Calendar.WEDNESDAY: return "星期三"; case Calendar.THURSDAY: return "星期四"; case Calendar.FRIDAY: return "星期五"; case Calendar.SATURDAY: return "星期六"; } return ""; } /** * * @Title: getWeekOfMonth * @Description: TODO(获取当前日期是本月的第几周) * @author fyf * @return */ public static String getWeekOfMonth() { Calendar calendar = Calendar.getInstance(); switch (calendar.get(Calendar.WEEK_OF_MONTH)) { case 1: return "第一周"; case 2: return "第二周"; case 3: return "第三周"; case 4: return "第四周"; case 5: return "第五周"; } return ""; } /** * * @Title: getWeekOfMonthFromeDate * @Description: TODO(获取该日期是当年当月的第几周) * @author fyf * @param date * @return */ public static String getWeekOfMonthFromeDate(String date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(getYyyyMMddFromString(date)); switch (calendar.get(Calendar.WEEK_OF_MONTH)) { case 1: return "第一周"; case 2: return "第二周"; case 3: return "第三周"; case 4: return "第四周"; case 5: return "第五周"; } return ""; } /** * * @Title: getWeekOfYear * @Description: TODO(获取当前日期是当年的第几周) * @author fyf * @return */ public static String getWeekOfYear() { Calendar calendar = Calendar.getInstance(); return "第" + (calendar.get(Calendar.WEEK_OF_YEAR)) + "周"; } /** * * @Title: getWeekOfYearFromString * @Description: TODO(获取该日期是当年的第几周) * @author fyf * @param date * @return */ public static String getWeekOfYearFromString(String date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(getYyyyMMddFromString(date)); return "第" + (calendar.get(Calendar.WEEK_OF_YEAR)) + "周"; } /** * * @Title: getYyyyMMdd * @Description: TODO(获取当前日期:2015-06-16) * @author fyf * @return */ public static String getYyyyMMdd() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(new Date()); } /** * * @Title: getYyyyMMddFromDate * @Description: TODO(转换日期格式:2015-06-16) * @author fyf * @param date * @return */ public static String getYyyyMMddFromDate(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if (date == null) return getYyyyMMdd(); return sdf.format(date); } /** * * @Title: getYyyyMMddFromString * @Description: TODO(转换日期格式:2015-06-16) * @author fyf * @param date * @return */ public static Date getYyyyMMddFromString(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if (date == null || "".equals(date)) date = getYyyyMMdd(); try { return sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * * @Title: getYmdHms * @Description: TODO(获取当前日期时间:2015-06-16 14:07:06) * @author fyf * @return */ public static String getYmdHms() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date()); } /** * * @Title: getYmdHmsFromDate * @Description: TODO(转换日期时间格式:2015-06-16 14:07:06) * @author fyf * @param date * @return */ public static String getYmdHmsFromDate(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (date == null) return getYmdHms(); return sdf.format(date); } /** * * @Title: getYmdHmsFromString * @Description: TODO(转换日期时间格式:2015-06-16 14:07:06) * @author fyf * @param date * @return */ public static Date getYmdHmsFromString(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (date == null || "".equals(date)) date = getYmdHms(); try { return sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * * @Title: getYmdHmsSSS * @Description: TODO(获取当前日期时间:20150616141020101) * @author fyf * @return */ public static String getYmdHmsSSS() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); return sdf.format(new Date()); } /** * * @Title: getYmdHmsSSSFromDate * @Description: TODO(转换日期时间格式:20150616141020101) * @author fyf * @param date * @return */ public static String getYmdHmsSSSFromDate(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); if (date == null) return getYmdHmsSSS(); return sdf.format(date); } /** * * @Title: getYmdHmsSSSFromString * @Description: TODO(转换日期时间格式:20150616141020101) * @author fyf * @param date * @return */ public static Date getYmdHmsSSSFromString(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); if (date == null || "".equals(date)) date = getYmdHmsSSS(); try { return sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * * @return yyyy年MM月dd日 */ public static String getYyyy_MM_dd() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); return sdf.format(new Date()); } /** * * @param date * @return yyyy年MM月dd日 */ public static String getYyyy_MM_ddFromDate(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); if (date == null) return sdf.format(new Date()); return sdf.format(date); } /** * * @param date * @return yyyy年MM月dd日 */ public static Date getYyyy_MM_ddFromString(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); if (date == null || "".equals(date)) date = getYyyy_MM_dd(); try { return sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * * @return yyyy年MM月dd日 HH时mm分ss秒 */ public static String getYmd_Hms() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); return sdf.format(new Date()); } /** * * @param date * @return yyyy年MM月dd日 HH时mm分ss秒 */ public static String getYmd_HmsFromDate(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); if (date == null) return sdf.format(new Date()); return sdf.format(date); } /** * * @param date * @return yyyy年MM月dd日 HH时mm分ss秒 */ public static Date getYmd_HmsFromString(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); if (date == null || "".equals(date)) date = getYmd_Hms(); try { return sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * * @Title: getMondayOfWeek * @Description: TODO(根据日期获取当周的星期一的日期) * @return */ public static String getMondayDateFromString(String date) { // 0:表示本周; 1:表示下周;-1:表示上周 int n = 0; Calendar cal = Calendar.getInstance(); cal.setTime(getYyyyMMddFromString(date)); cal.add(Calendar.DATE, n * 7); // 想转星期几, Calendar就引用星期几(星期三:Calendar.WEDNESDAY) cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return getYyyyMMddFromDate(cal.getTime()); } /** * * @Title: getMondayOfWeek * @Description: TODO(根据日期获取当周的星期一的日期) * @return */ public static String getMondayDateFromDate(Date date) { // 0:表示本周; 1:表示下周;-1:表示上周 int n = 0; Calendar cal = Calendar.getInstance(); if (date == null) date = new Date(); cal.setTime(date); cal.add(Calendar.DATE, n * 7); // 想转星期几, Calendar就引用星期几(星期三:Calendar.WEDNESDAY) cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return getYyyyMMddFromDate(cal.getTime()); } }