1. 程式人生 > >Android-DateTimeAndroidUtil-工具類

Android-DateTimeAndroidUtil-工具類

DateTimeAndroidUtil-工具類 是關於時間日前相關的公用方法;

 

package liudeli.mynetwork01.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @Author Liudeli
 * @Describe:時間相關工具類
 
*/ public class DateTimeAndroidUtil { private DateTimeAndroidUtil(){} /** * 列舉日期格式 */ public enum DatePattern{ /** * 格式:"yyyy-MM-dd HH:mm:ss" */ ALL_TIME{public String getValue(){return "yyyy-MM-dd HH:mm:ss";}}, /** * 格式:"yyyy-MM"
*/ ONLY_MONTH{public String getValue(){return "yyyy-MM";}}, /** * 格式:"yyyy-MM-dd" */ ONLY_DAY{public String getValue(){return "yyyy-MM-dd";}}, /** * 格式:"yyyy-MM-dd HH" */ ONLY_HOUR{public String getValue(){return "yyyy-MM-dd HH";}},
/** * 格式:"yyyy-MM-dd HH:mm" */ ONLY_MINUTE{public String getValue(){return "yyyy-MM-dd HH:mm";}}, /** * 格式:"MM-dd" */ ONLY_MONTH_DAY{public String getValue(){return "MM-dd";}}, /** * 格式:"MM-dd HH:mm" */ ONLY_MONTH_SEC{public String getValue(){return "MM-dd HH:mm";}}, /** * 格式:"HH:mm:ss" */ ONLY_TIME{public String getValue(){return "HH:mm:ss";}}, /** * 格式:"HH:mm" */ ONLY_HOUR_MINUTE{public String getValue(){return "HH:mm";}}; public abstract String getValue(); } /** * 獲取當前時間 * @return 返回當前時間,格式2017-05-04 10:54:21 */ public static String getNowDate(DatePattern pattern){ String dateString = null; Calendar calendar = Calendar.getInstance(); Date dateNow = calendar.getTime(); SimpleDateFormat sdf = new SimpleDateFormat(pattern.getValue(),Locale.CHINA); dateString = sdf.format(dateNow); return dateString; } /** * 將一個日期字串轉換成Data物件 * @param dateString 日期字串 * @param pattern 轉換格式 * @return 返回轉換後的日期物件 */ public static Date stringToDate(String dateString, DatePattern pattern){ Date date = null; SimpleDateFormat sdf = new SimpleDateFormat(pattern.getValue(),Locale.CHINA); try { date = sdf.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * 將date轉換成字串 * @param date 日期 * @param pattern 日期的目標格式 * @return */ public static String dateToString(Date date, DatePattern pattern){ String string = ""; SimpleDateFormat sdf = new SimpleDateFormat(pattern.getValue(), Locale.CHINA); string = sdf.format(date); return string; } /** * 獲取指定日期周幾 * @param date 指定日期 * @return * 返回值為: "週日", "週一", "週二", "週三", "週四", "週五", "週六" */ public static String getWeekOfDate(Date date){ String[] weekDays = { "週日", "週一", "週二", "週三", "週四", "週五", "週六" }; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; if (week < 0) week = 0; return weekDays[week]; } /** * 獲取指定日期對應周幾的序列 * @param date 指定日期 * @return 週一:1 週二:2 週三:3 週四:4 週五:5 週六:6 週日:7 */ public static int getIndexWeekOfDate(Date date){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int index = calendar.get(Calendar.DAY_OF_WEEK); if(index == 1){ return 7; }else{ return --index; } } /** * 返回當前月份 * @return */ public static int getNowMonth(){ Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.MONTH) + 1; } /** * 獲取當前月號 * @return */ public static int getNowDay(){ Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.DATE); } /** * 獲取當前年份 * @return */ public static int getNowYear(){ Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.YEAR); } /** * 獲取本月份的天數 * @return */ public static int getNowDaysOfMonth(){ Calendar calendar = Calendar.getInstance(); return daysOfMonth(calendar.get(Calendar.YEAR),calendar.get(Calendar.DATE) + 1); } /** * 獲取指定月份的天數 * @param year 年份 * @param month 月份 * @return 對應天數 */ public static int daysOfMonth(int year,int 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; } } /** * 獲取當前時間 * @return */ public static String getNowTime(){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(System.currentTimeMillis()); return simpleDateFormat.format(date); } /** * 獲取當前時間 * @return */ public static String getThisTiem() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss // 獲取當前時間 Date date = new Date(System.currentTimeMillis()); return simpleDateFormat.format(date); } /** * 獲取時間戳 * * @return 獲取時間戳 */ public static String getTimeString() { SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Calendar calendar = Calendar.getInstance(); return df.format(calendar.getTime()); } /** * 獲取時間戳 * * @return 獲取時間戳 */ public static String getTimeString2() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); return df.format(calendar.getTime()); } /** * 時間轉換為時間戳 * @param time:需要轉換的時間 * @return */ public static String dateToStamp(String time) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = simpleDateFormat.parse(time); } catch (ParseException e) { e.printStackTrace(); } long ts = date.getTime(); return String.valueOf(ts); } /** * 時間戳轉換為字串 * @param time:時間戳 * @return */ public static String times(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } /** *獲取距現在某一小時的時刻 * @param hour hour=-1為上一個小時,hour=1為下一個小時 * @return */ public static String getLongTime(int hour){ Calendar c = Calendar.getInstance(); // 當時的日期和時間 int h; // 需要更改的小時 h = c.get(Calendar.HOUR_OF_DAY) - hour; c.set(Calendar.HOUR_OF_DAY, h); SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Log.v("time",df.format(c.getTime())); return df.format(c.getTime()); } /** * 比較時間大小 * @param str1:要比較的時間 * @param str2:要比較的時間 * @return */ public static boolean isDateOneBigger(String str1, String str2) { boolean isBigger = false; SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Date dt1 = null; Date dt2 = null; try { dt1 = sdf.parse(str1); dt2 = sdf.parse(str2); } catch (ParseException e) { e.printStackTrace(); } if (dt1.getTime() > dt2.getTime()) { isBigger = true; } else if (dt1.getTime() < dt2.getTime()) { isBigger = false; } return isBigger; } /** * 當地時間 ---> UTC時間 * @return */ public static String Local2UTC(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("gmt")); String gmtTime = sdf.format(new Date()); return gmtTime; } /** * UTC時間 ---> 當地時間 * @param utcTime UTC時間 * @return */ public static String utc2Local(String utcTime) { SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC時間格式 utcFormater.setTimeZone(TimeZone.getTimeZone("UTC")); Date gpsUTCDate = null; try { gpsUTCDate = utcFormater.parse(utcTime); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//當地時間格式 localFormater.setTimeZone(TimeZone.getDefault()); String localTime = localFormater.format(gpsUTCDate.getTime()); return localTime; } }