import android.app.AlarmManager;
import android.content.Context;
-import android.os.SystemClock;
import android.provider.Settings;
import android.text.format.DateFormat;
-import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import static android.content.Context.ALARM_SERVICE;
public class TimeUtil {
- public static String getLocalTime() { return getLocalTime(System.currentTimeMillis()); }
-
- public static String getLocalTime(long paramLong) { return (new SimpleDateFormat("yyyy/MM/dd,HH:mm:ss")).format(new Date(paramLong)); }
- public static String getLocalTime2() { return getLocalTime2(System.currentTimeMillis()); }
-
- public static String getLocalTime2(long paramLong) { return (new SimpleDateFormat("MMddHHmmss")).format(new Date(paramLong)); }
-
- public static String getSystemTimeForCameraMark() { return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(System.currentTimeMillis())); }
-
- public static String getSystemTime() { return getSystemTime(System.currentTimeMillis()); }
-
- //此处不能随意改动,影响闹铃设置
- public static String getSystemTime(long paramLong) { return (new SimpleDateFormat("yyyy-MM-dd;HH:mm:ss")).format(new Date(paramLong)); }
-
- public static String getSystemTime2() { return getSystemTime2(System.currentTimeMillis()); }
-
- public static String getSystemTime2(long paramLong) { return (new SimpleDateFormat("yyyyMMdd;HHmmss")).format(new Date(paramLong)); }
-
- public static String getSystemTimeyyyyMMddHHmmss() { return getSystemTime3(System.currentTimeMillis()); }
- public static String getSystemTime3() { return getSystemTime3(System.currentTimeMillis()); }
-
- public static String getSystemTime3(long paramLong) { return (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date(paramLong)); }
-
- public static String getSystemTime4() { return getSystemTime4(System.currentTimeMillis()); }
-
- public static String getSystemTime4(long paramLong) { return (new SimpleDateFormat("yyyyMMdd")).format(new Date(paramLong)); }
-
- public static String getSystemDateYYMMDD(){
- return (new SimpleDateFormat("yyMMdd")).format(new Date(System.currentTimeMillis()));
- }
- public static String getSystemTimeHHmmss(){
- return (new SimpleDateFormat("HHmmss")).format(new Date(System.currentTimeMillis()));
- }
-
- public static String getUTCSystemDateYYMMDD(){
- return (new SimpleDateFormat("yyMMdd")).format(new Date(System.currentTimeMillis()-TimeZone.getDefault().getRawOffset()));
- }
- public static String getUTCSystemTimeHHmmss(){
- return (new SimpleDateFormat("HHmmss")).format(new Date(System.currentTimeMillis()-TimeZone.getDefault().getRawOffset()));
- }
- public static Date parseDatetiime(String date,String time){
+ public static String getLocalTime() {
+ return getLocalTime(System.currentTimeMillis());
+ }
+
+ public static String getLocalTime(long paramLong) {
+ return (new SimpleDateFormat("yyyy/MM/dd,HH:mm:ss")).format(new Date(paramLong));
+ }
+
+ public static String getLocalTime2() {
+ return getLocalTime2(System.currentTimeMillis());
+ }
+
+ public static String getLocalTime2(long paramLong) {
+ return (new SimpleDateFormat("MMddHHmmss")).format(new Date(paramLong));
+ }
+
+ public static String getSystemTimeForCameraMark() {
+ return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(System.currentTimeMillis()));
+ }
+
+ public static String getSystemTime() {
+ return getSystemTime(System.currentTimeMillis());
+ }
+
+ //此处不能随意改动,影响闹铃设置
+ public static String getSystemTime(long paramLong) {
+ return (new SimpleDateFormat("yyyy-MM-dd;HH:mm:ss")).format(new Date(paramLong));
+ }
+
+ public static String getSystemTime2() {
+ return getSystemTime2(System.currentTimeMillis());
+ }
+
+ public static String getSystemTime2(long paramLong) {
+ return (new SimpleDateFormat("yyyyMMdd;HHmmss")).format(new Date(paramLong));
+ }
+
+ public static String getSystemTimeyyyyMMddHHmmss() {
+ return getSystemTime3(System.currentTimeMillis());
+ }
+
+ public static String getSystemTime3() {
+ return getSystemTime3(System.currentTimeMillis());
+ }
+
+ public static String getSystemTime3(long paramLong) {
+ return (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date(paramLong));
+ }
+
+ public static String getSystemTime4() {
+ return getSystemTime4(System.currentTimeMillis());
+ }
+
+ public static String getSystemTime4(long paramLong) {
+ return (new SimpleDateFormat("yyyyMMdd")).format(new Date(paramLong));
+ }
+
+ public static String getSystemDateYYMMDD() {
+ return (new SimpleDateFormat("yyMMdd")).format(new Date(System.currentTimeMillis()));
+ }
+
+ public static String getSystemTimeHHmmss() {
+ return (new SimpleDateFormat("HHmmss")).format(new Date(System.currentTimeMillis()));
+ }
+
+ public static String getUTCSystemDateYYMMDD() {
+ return (new SimpleDateFormat("yyMMdd")).format(new Date(System.currentTimeMillis() - TimeZone.getDefault().getRawOffset()));
+ }
+
+ public static String getUTCSystemTimeHHmmss() {
+ return (new SimpleDateFormat("HHmmss")).format(new Date(System.currentTimeMillis() - TimeZone.getDefault().getRawOffset()));
+ }
+
+ public static Date parseDatetiime(String date, String time) {
// SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);//输入的被转化的时间格式
- SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//需要转化成的时间格式 08:00:00,2016-01-12
+ SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//需要转化成的时间格式 08:00:00,2016-01-12
// SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss");
- Date date1 = null;
- try {
- String dt=date+" "+time;
- ICLogger.i("parseDatetiime " +dt);
- date1 = df1.parse(dt);
- return date1;
- } catch (ParseException e) {
- e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
- return null;
- }
+ Date date1 = null;
+ try {
+ String dt = date + " " + time;
+ ICLogger.i("parseDatetiime " + dt);
+ date1 = df1.parse(dt);
+ return date1;
+ } catch (ParseException e) {
+ e.printStackTrace();
+ ICLogger.e(e.getMessage(), e);
+ return null;
+ }
// String str1 = df1.format(date1);
// String str2 = df2.format(date1);
- }
- public static Date parseDatetiime(String datetimeStr){
+ }
+
+ public static Date parseDatetiime(String datetimeStr) {
// SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);//输入的被转化的时间格式
// SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//需要转化成的时间格式
// SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
- SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss");
- Date date1 = null;
- try {
- date1 = df3.parse(datetimeStr);
- return date1;
- } catch (ParseException e) {
- e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
- return null;
- }
+ SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss");
+ Date date1 = null;
+ try {
+ date1 = df3.parse(datetimeStr);
+ return date1;
+ } catch (ParseException e) {
+ e.printStackTrace();
+ ICLogger.e(e.getMessage(), e);
+ return null;
+ }
// String str1 = df1.format(date1);
// String str2 = df2.format(date1);
- }
- /**
- * 根据当前日期获得是星期几
- *
- * @return
- */
- public static String getWeek() {
- String time =getSystemTime4();
- String Week = "";
- SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
- Calendar c = Calendar.getInstance();
- try {
- c.setTime(format.parse(time));
- } catch (ParseException e) {
- e.printStackTrace();
- ICLogger.e(e.getMessage(),e);
- }
- if (c.get(Calendar.DAY_OF_WEEK) == 1) {
- Week += "周天";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == 2) {
- Week += "周一";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == 3) {
- Week += "周二";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == 4) {
- Week += "周三";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == 5) {
- Week += "周四";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == 6) {
- Week += "周五";
- }
- if (c.get(Calendar.DAY_OF_WEEK) == 7) {
- Week += "周六";
- }
- return Week;
- }
- /* 返回 2位年2位月2位日, 如2019-12-30 返回 191230*/
- public static String getShortDate() {
- String DEFAULT_TIME_FORMAT = "yyMMdd";
- SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
- String dateStr = dateFormatter.format(Calendar.getInstance().getTime());//获取当前时间
- return dateStr;
- }
- /*061830: 格林尼治时间06 点18 分30 秒 */
- public static String getShortTime(){
- String DEFAULT_TIME_FORMAT = "HHmmss";
- //设置时间的格式
- // yyyy-MM-dd 1969-12-31
- // yyyy-MM-dd 1970-01-01
- // yyyy-MM-dd HH:mm 1969-12-31 16:00
- // yyyy-MM-dd HH:mm 1970-01-01 00:00
- // yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
- // yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
- // yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
- // yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
- // yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
- // yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000
- //注:HH显示的是24小时制,hh显示的是12小时制
-
- SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
- String dateStr = dateFormatter.format(Calendar.getInstance().getTime());//获取当前时间
+ }
+
+ /**
+ * 根据当前日期获得是星期几
+ *
+ * @return
+ */
+ public static String getWeek() {
+ String time = getSystemTime4();
+ String Week = "";
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
+ Calendar c = Calendar.getInstance();
+ try {
+ c.setTime(format.parse(time));
+ } catch (ParseException e) {
+ e.printStackTrace();
+ ICLogger.e(e.getMessage(), e);
+ }
+ if (c.get(Calendar.DAY_OF_WEEK) == 1) {
+ Week += "周天";
+ }
+ if (c.get(Calendar.DAY_OF_WEEK) == 2) {
+ Week += "周一";
+ }
+ if (c.get(Calendar.DAY_OF_WEEK) == 3) {
+ Week += "周二";
+ }
+ if (c.get(Calendar.DAY_OF_WEEK) == 4) {
+ Week += "周三";
+ }
+ if (c.get(Calendar.DAY_OF_WEEK) == 5) {
+ Week += "周四";
+ }
+ if (c.get(Calendar.DAY_OF_WEEK) == 6) {
+ Week += "周五";
+ }
+ if (c.get(Calendar.DAY_OF_WEEK) == 7) {
+ Week += "周六";
+ }
+ return Week;
+ }
+
+ /* 返回 2位年2位月2位日, 如2019-12-30 返回 191230*/
+ public static String getShortDate() {
+ String DEFAULT_TIME_FORMAT = "yyMMdd";
+ SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
+ String dateStr = dateFormatter.format(Calendar.getInstance().getTime());//获取当前时间
+ return dateStr;
+ }
+
+ /*061830: 格林尼治时间06 点18 分30 秒 */
+ public static String getShortTime() {
+ String DEFAULT_TIME_FORMAT = "HHmmss";
+ //设置时间的格式
+ // yyyy-MM-dd 1969-12-31
+ // yyyy-MM-dd 1970-01-01
+ // yyyy-MM-dd HH:mm 1969-12-31 16:00
+ // yyyy-MM-dd HH:mm 1970-01-01 00:00
+ // yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
+ // yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
+ // yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
+ // yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
+ // yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
+ // yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000
+ //注:HH显示的是24小时制,hh显示的是12小时制
+
+ SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
+ String dateStr = dateFormatter.format(Calendar.getInstance().getTime());//获取当前时间
// SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);//输入的被转化的时间格式
// SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//需要转化成的时间格式
// SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
- return dateStr;
- }
-
- public static boolean is24Hour(Context mContext){
- return DateFormat.is24HourFormat(mContext);
- }
-
- public static void set24Hour(Context mContext,boolean _is24Hour){
- if (_is24Hour) {
- android.provider.Settings.System.putString(mContext.getContentResolver(),
- android.provider.Settings.System.TIME_12_24, "24");
- }else{
- android.provider.Settings.System.putString(mContext.getContentResolver(),
- android.provider.Settings.System.TIME_12_24, "12");
- }
- }
-
- public static boolean isTimeZoneAuto(Context mContext){
- try {
- return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
- android.provider.Settings.Global.AUTO_TIME_ZONE) > 0;
- } catch (Settings.SettingNotFoundException e) {
- e.printStackTrace();
- return false;
- }
- }
- public static void setAutoTimeZone(Context mContext,int checked){
- android.provider.Settings.Global.putInt(mContext.getContentResolver(),
- android.provider.Settings.Global.AUTO_TIME_ZONE, checked);
- }
-
- public static boolean isDateTimeAuto(Context mContext){
- try {
- return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
- android.provider.Settings.Global.AUTO_TIME) > 0;
- } catch (Settings.SettingNotFoundException e) {
- e.printStackTrace();
- return false;
- }
- }
-
- public static void setAutoDateTime(Context mContext,int checked){
- android.provider.Settings.Global.putInt(mContext.getContentResolver(),
- android.provider.Settings.Global.AUTO_TIME, checked);
- }
-
- public static void setSysDateTime(Context mContext,Date datetime,int timezone,boolean addtimezoneToTime){
-
- AlarmManager alarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
- Locale.getDefault().getDisplayCountry();
- String timezone_name="";
- if(timezone>0) {
- timezone_name=String.format("GMT+%1$02d:00", Math.abs(timezone));
-
- }else{
- timezone_name=String.format("GMT-%1$02d:00", Math.abs(timezone));
- }
- TimeZone tz = TimeZone.getTimeZone(timezone_name);
- ICLogger.i("====set timezone "+tz.getDisplayName());
-
- Calendar c = Calendar.getInstance();
-
- ICLogger.i("====origin time " + c.getTime().toString()+" time zone "+c.getTimeZone());
- c.setTime(datetime);
- if (addtimezoneToTime) {
- if (is24Hour(mContext)) {
- c.add(Calendar.HOUR_OF_DAY, timezone);//24小时制
- } else {
- c.add(Calendar.HOUR, timezone);//12小时制
- }
- c.setTimeZone(tz);
+ return dateStr;
}
-// alarmManager.setTimeZone();
- long when = c.getTimeInMillis();
- if (when / 1000 < Integer.MAX_VALUE) {
- //SystemClock.setCurrentTimeMillis(when);
- alarmManager.setTime(when);
+ public static boolean is24Hour(Context mContext) {
+ return DateFormat.is24HourFormat(mContext);
+ }
+
+ public static void set24Hour(Context mContext, boolean _is24Hour) {
+ if (_is24Hour) {
+ android.provider.Settings.System.putString(mContext.getContentResolver(),
+ android.provider.Settings.System.TIME_12_24, "24");
+ } else {
+ android.provider.Settings.System.putString(mContext.getContentResolver(),
+ android.provider.Settings.System.TIME_12_24, "12");
+ }
+ }
+
+ public static boolean isTimeZoneAuto(Context mContext) {
+ try {
+ return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
+ android.provider.Settings.Global.AUTO_TIME_ZONE) > 0;
+ } catch (Settings.SettingNotFoundException e) {
+ e.printStackTrace();
+ return false;
+ }
}
- Calendar d=Calendar.getInstance();
- long now = d.getTimeInMillis();
- //Log.d(TAG, "set tm="+when + ", now tm="+now);
+ public static void setAutoTimeZone(Context mContext, int checked) {
+ android.provider.Settings.Global.putInt(mContext.getContentResolver(),
+ android.provider.Settings.Global.AUTO_TIME_ZONE, checked);
+ }
+
+ public static boolean isDateTimeAuto(Context mContext) {
+ try {
+ return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
+ android.provider.Settings.Global.AUTO_TIME) > 0;
+ } catch (Settings.SettingNotFoundException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
- if(now - when > 1000)
- {
- ICLogger.i("====set time failed now("+now+")-when("+when+") > 1000 !");
+ public static void setAutoDateTime(Context mContext, int checked) {
+ android.provider.Settings.Global.putInt(mContext.getContentResolver(),
+ android.provider.Settings.Global.AUTO_TIME, checked);
}
- ICLogger.i("====set time to " + d.getTime().toString()+" time zone "+d.getTimeZone());
+ public static void setSysDateTime(Context mContext, Date datetime, int timezone, boolean addtimezoneToTime) {
+
+ AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
+ Locale.getDefault().getDisplayCountry();
+ String timezone_name = "";
+ if (timezone > 0) {
+ timezone_name = String.format("GMT+%1$02d:00", Math.abs(timezone));
+
+ } else {
+ timezone_name = String.format("GMT-%1$02d:00", Math.abs(timezone));
+ }
+ TimeZone tz = TimeZone.getTimeZone(timezone_name);
+ ICLogger.i("====set timezone " + tz.getDisplayName());
+
+ Calendar c = Calendar.getInstance();
+
+ ICLogger.i("====origin time " + c.getTime().toString() + " time zone " + c.getTimeZone());
+ c.setTime(datetime);
+ if (addtimezoneToTime) {
+ if (is24Hour(mContext)) {
+ c.add(Calendar.HOUR_OF_DAY, timezone);//24小时制
+ } else {
+ c.add(Calendar.HOUR, timezone);//12小时制
+ }
+ c.setTimeZone(tz);
+ }
- }
- public static void setSysDateTime(Context mContext,Date datetime,int timezone){
+// alarmManager.setTimeZone();
+ long when = c.getTimeInMillis();
+ if (when / 1000 < Integer.MAX_VALUE) {
+ //SystemClock.setCurrentTimeMillis(when);
+ alarmManager.setTime(when);
+ }
- setSysDateTime(mContext,datetime,timezone,true);
+ Calendar d = Calendar.getInstance();
+ long now = d.getTimeInMillis();
+ //Log.d(TAG, "set tm="+when + ", now tm="+now);
- }
+ if (now - when > 1000) {
+ ICLogger.i("====set time failed now(" + now + ")-when(" + when + ") > 1000 !");
+ }
- public static void setSysTime(Context mContext,int hour,int minute,int secounds){
- Calendar c = Calendar.getInstance();
- c.set(Calendar.HOUR_OF_DAY, hour);
- c.set(Calendar.MINUTE, minute);
- c.set(Calendar.SECOND, secounds);
- c.set(Calendar.MILLISECOND, 0);
+ ICLogger.i("====set time to " + d.getTime().toString() + " time zone " + d.getTimeZone());
- long when = c.getTimeInMillis();
- ICLogger.i("====set time "+c.toString());
- if(when / 1000 < Integer.MAX_VALUE){
- ((AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when);
}
- }
- // 获取 “GMT+08:00”对应的时区
+ public static void setSysDateTime(Context mContext, Date datetime, int timezone) {
+
+ setSysDateTime(mContext, datetime, timezone, true);
+
+ }
+
+ public static void setSysTime(Context mContext, int hour, int minute, int secounds) {
+ Calendar c = Calendar.getInstance();
+ c.set(Calendar.HOUR_OF_DAY, hour);
+ c.set(Calendar.MINUTE, minute);
+ c.set(Calendar.SECOND, secounds);
+ c.set(Calendar.MILLISECOND, 0);
+
+ long when = c.getTimeInMillis();
+ ICLogger.i("====set time " + c.toString());
+ if (when / 1000 < Integer.MAX_VALUE) {
+ ((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when);
+ }
+ }
+
+ // 获取 “GMT+08:00”对应的时区
// TimeZone china = TimeZone.getTimeZone("GMT+:08:00");
- // 获取 “中国/重庆”对应的时区
+ // 获取 “中国/重庆”对应的时区
// TimeZone chongqing = TimeZone.getTimeZone("Asia/Chongqing");
- public static void setTimeZone(String timeZone){
- final Calendar now = Calendar.getInstance();
- TimeZone tz = TimeZone.getTimeZone(timeZone);
- now.setTimeZone(tz);
- }
- public static void setTimeZone(Context context,int timeZone) {
-
- final Calendar now = Calendar.getInstance();
- String timezone_name="";
- if(timeZone>0) {
- timezone_name=String.format("GMT+%1$02d:00", Math.abs(timeZone));
-
- }else{
- timezone_name=String.format("GMT-%1$02d:00", Math.abs(timeZone));
- }
- ICLogger.i("====set timezone "+timezone_name);
- //TimeZone tz = TimeZone.getTimeZone(timezone_name);
- updateTimeZone(context,timezone_name);
- }
-
- public static void setTimeZone(int timeZone){
- final Calendar now = Calendar.getInstance();
- String timezone_name="";
- if(timeZone>0) {
- timezone_name=String.format("GMT+%1$02d:00", Math.abs(timeZone));
+ public static void setTimeZone(String timeZone) {
+ final Calendar now = Calendar.getInstance();
+ TimeZone tz = TimeZone.getTimeZone(timeZone);
+ now.setTimeZone(tz);
+ }
+
+ public static void setTimeZone(Context context, int timeZone) {
+
+ final Calendar now = Calendar.getInstance();
+ String timezone_name = "";
+ if (timeZone > 0) {
+ timezone_name = String.format("GMT+%1$02d:00", Math.abs(timeZone));
+
+ } else {
+ timezone_name = String.format("GMT-%1$02d:00", Math.abs(timeZone));
+ }
+ ICLogger.i("====set timezone " + timezone_name);
+ //TimeZone tz = TimeZone.getTimeZone(timezone_name);
+ updateTimeZone(context, timezone_name);
+ }
+
+ public static void setTimeZone(int timeZone) {
+ final Calendar now = Calendar.getInstance();
+ String timezone_name = "";
+ if (timeZone > 0) {
+ timezone_name = String.format("GMT+%1$02d:00", Math.abs(timeZone));
+
+ } else {
+ timezone_name = String.format("GMT-%1$02d:00", Math.abs(timeZone));
+ }
+ ICLogger.i("====set timezone " + timezone_name);
+ TimeZone tz = TimeZone.getTimeZone(timezone_name);
+ now.setTimeZone(tz);
+ TimeZone.setDefault(tz);
- }else{
- timezone_name=String.format("GMT-%1$02d:00", Math.abs(timeZone));
}
- ICLogger.i("====set timezone "+timezone_name);
- TimeZone tz = TimeZone.getTimeZone(timezone_name);
- now.setTimeZone(tz);
- TimeZone.setDefault(tz);
- }
- public static String getDefaultTimeZone(){
- return TimeZone.getDefault().getDisplayName();
- }
-
- private static long HOURS12 = 12*60*60*1000 ;
- private static long HOURS24 = 24*60*60*1000 ;
-
- public static String getCurrentSleepRecordDate(){
- return getCurrentSleepRecordDate(System.currentTimeMillis());
- }
- public static String getCurrentSleepRecordDate(String date_yyyyMMddHHmmss){
- SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss");
- Date date1 = null;
- try {
- //获取当天的开始睡眠检测时间
- date1 = df3.parse(date_yyyyMMddHHmmss);
- } catch (ParseException e) {
- e.printStackTrace();
- return null;
- }
- return getCurrentSleepRecordDate(date1.getTime());
- }
- public static boolean isInSleepRange=false;
- public static String getCurrentSleepRecordDate(long currentMS){
- //睡眠检测时间跨天,所以计算要注意日期问题。
- //默认时间段是当天的晚上21点到次日的上午9点。跨度12小时
-
- /** currentDate yyyyMMdd */
- String currentDate=getSystemTime4(currentMS);
- SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss");
-
- Date date1 = null;
- try {
- //获取当天的开始睡眠检测时间
- date1 = df3.parse(currentDate+"210000");
- } catch (ParseException e) {
- e.printStackTrace();
- }
-
- if (date1!=null) {
- //当天开始睡眠检测时间
- long sleep_check_starttime= date1.getTime();
-
- long sleep_check_endtime=sleep_check_starttime+HOURS12;
-
-
- long sleep_check_starttime_yestoday = sleep_check_starttime - HOURS24;
- long sleep_check_endtime_yestoday=sleep_check_starttime_yestoday+HOURS12;
-
- if (currentMS > sleep_check_starttime && currentMS<sleep_check_endtime) {
- //在当天的睡眠 检测阶段中,不上报
- isInSleepRange=true;
- return getSystemTime4(sleep_check_starttime);
- }else if (currentMS > sleep_check_starttime_yestoday && currentMS <sleep_check_endtime_yestoday ){
- //在前一天的睡眠检测阶段中,不上报
- isInSleepRange=true;
- return getSystemTime4(sleep_check_starttime_yestoday);
- }else if (currentMS >sleep_check_endtime_yestoday && currentMS <sleep_check_starttime){
- //前一天的睡眠检测结束,当天睡眠检测还没开始,上报前一天的数据
- //返回 null
- isInSleepRange=false;
- return getSystemTime4(sleep_check_starttime_yestoday);
- }
-
- }
- return null;
- }
-
- public static long diffBetween2DateInMinutes(String dateBefore_yyyyMMDDhhmmss, String dateAfter_yyyyMMDDhhmmss){
- long date1_ms=parseDatetiime(dateBefore_yyyyMMDDhhmmss).getTime();
- long date2_ms=parseDatetiime(dateAfter_yyyyMMDDhhmmss).getTime();
- return (date2_ms-date1_ms)/60000 ;
- }
-
-
-
-
- public static void testUTCSystemtime(){
-
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
-
-
- long currentTime=System.currentTimeMillis()-TimeZone.getDefault().getRawOffset();
- ICLogger.i("UTC currentTime = "+df.format(new Date(currentTime)));
- currentTime=Calendar.getInstance().getTimeInMillis();
- ICLogger.i("currentTime = "+df.format(new Date(currentTime)));
- currentTime=new Date().getTime();
-
-
-
- }
-
- static String getTimeZoneID_ByDisplayName(String tzname) {
- String str = TimeZone.getDefault().getID();
- if (tzname.equals("GMT+03:30")) {
- str = "Asia/Tehran";
- } else if (tzname.equals("GMT+04:30")) {
- str = "Asia/Kabul";
- } else if (tzname.equals("GMT+05:30")) {
- str = "Asia/Colombo";
- } else if (tzname.equals("GMT+06:30")) {
- str = "Asia/Rangoon";
- } else if (tzname.equals("GMT+09:30")) {
- str = "Australia/Darwin";
- } else if (tzname.equals("GMT+09:00"))
- {
- str = "Asia/Tokyo";
- }
- return str;
- }
- static void updateTimeZone(Context context,String paramString) {
- AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
- ICLogger.d("updateTimeZone:" + paramString);
- if (paramString == null) {
- ICLogger.d("updateTimeZone name is null");
- return;
- }
- String[] arrayOfString = TimeZone.getAvailableIDs();
- TimeZone timeZone = TimeZone.getDefault();
- boolean bool = false;
- //遍历所有支持的ID,如果字串相同,则设置对应的timezone,否则直接用字符串进行设置
- for(int b=0;b<arrayOfString.length;b++){
- String tztmp =arrayOfString[b];
- TimeZone timeZone1=TimeZone.getTimeZone(tztmp);
- String tzid=timeZone1.getID();
- // ICLogger.d(b+"/"+arrayOfString.length+" "+paramString+"== "+tztmp+"? ID="+tzid);
- if (paramString.equals(timeZone1.getDisplayName(false,TimeZone.SHORT)) && timeZone1.getID().contains("Etc/GMT")) {
-
- String str1 = timeZone1.getTimeZone(tztmp).getDisplayName();
- String str2 = TimeZone.getTimeZone(tztmp).getID();
-
- str1 = "I have find timeZoneDisplayName:" + str1 + " ID:" + str2;
- ICLogger.d("===tmp:" + str1);
- ICLogger.d("setTimeZone:" + timeZone1.getDisplayName());
- alarmManager.setTimeZone(timeZone1.getID());
- return;
- }
- }
- ICLogger.d(paramString+" not found , set to default timezone "+timeZone.getID());
- alarmManager.setTimeZone(getTimeZoneID_ByDisplayName(paramString));
- }
+ public static String getDefaultTimeZone() {
+ return TimeZone.getDefault().getDisplayName();
+ }
+
+ private static long HOURS12 = 12 * 60 * 60 * 1000;
+ private static long HOURS24 = 24 * 60 * 60 * 1000;
+
+ public static String getCurrentSleepRecordDate() {
+ return getCurrentSleepRecordDate(System.currentTimeMillis());
+ }
+
+ public static String getCurrentSleepRecordDate(String date_yyyyMMddHHmmss) {
+ SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss");
+ Date date1 = null;
+ try {
+ //获取当天的开始睡眠检测时间
+ date1 = df3.parse(date_yyyyMMddHHmmss);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ return null;
+ }
+ return getCurrentSleepRecordDate(date1.getTime());
+ }
+
+ public static boolean isInSleepRange = false;
+
+ public static String getCurrentSleepRecordDate(long currentMS) {
+ //睡眠检测时间跨天,所以计算要注意日期问题。
+ //默认时间段是当天的晚上21点到次日的上午9点。跨度12小时
+
+ /** currentDate yyyyMMdd */
+ String currentDate = getSystemTime4(currentMS);
+ SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss");
+
+ Date date1 = null;
+ try {
+ //获取当天的开始睡眠检测时间
+ date1 = df3.parse(currentDate + "210000");
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+
+ if (date1 != null) {
+ //当天开始睡眠检测时间
+ long sleep_check_starttime = date1.getTime();
+
+ long sleep_check_endtime = sleep_check_starttime + HOURS12;
+
+
+ long sleep_check_starttime_yestoday = sleep_check_starttime - HOURS24;
+ long sleep_check_endtime_yestoday = sleep_check_starttime_yestoday + HOURS12;
+
+ if (currentMS > sleep_check_starttime && currentMS < sleep_check_endtime) {
+ //在当天的睡眠 检测阶段中,不上报
+ isInSleepRange = true;
+ return getSystemTime4(sleep_check_starttime);
+ } else if (currentMS > sleep_check_starttime_yestoday && currentMS < sleep_check_endtime_yestoday) {
+ //在前一天的睡眠检测阶段中,不上报
+ isInSleepRange = true;
+ return getSystemTime4(sleep_check_starttime_yestoday);
+ } else if (currentMS > sleep_check_endtime_yestoday && currentMS < sleep_check_starttime) {
+ //前一天的睡眠检测结束,当天睡眠检测还没开始,上报前一天的数据
+ //返回 null
+ isInSleepRange = false;
+ return getSystemTime4(sleep_check_starttime_yestoday);
+ }
+
+ }
+ return null;
+ }
+
+ public static long diffBetween2DateInMinutes(String dateBefore_yyyyMMDDhhmmss, String dateAfter_yyyyMMDDhhmmss) {
+ long date1_ms = parseDatetiime(dateBefore_yyyyMMDDhhmmss).getTime();
+ long date2_ms = parseDatetiime(dateAfter_yyyyMMDDhhmmss).getTime();
+ return (date2_ms - date1_ms) / 60000;
+ }
+
+
+ public static void testUTCSystemtime() {
+
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
+
+
+ long currentTime = System.currentTimeMillis() - TimeZone.getDefault().getRawOffset();
+ ICLogger.i("UTC currentTime = " + df.format(new Date(currentTime)));
+ currentTime = Calendar.getInstance().getTimeInMillis();
+ ICLogger.i("currentTime = " + df.format(new Date(currentTime)));
+ currentTime = new Date().getTime();
+
+
+ }
+
+ static String getTimeZoneID_ByDisplayName(String tzname) {
+ String str = TimeZone.getDefault().getID();
+ if (tzname.equals("GMT+03:30")) {
+ str = "Asia/Tehran";
+ } else if (tzname.equals("GMT+04:30")) {
+ str = "Asia/Kabul";
+ } else if (tzname.equals("GMT+05:30")) {
+ str = "Asia/Colombo";
+ } else if (tzname.equals("GMT+06:30")) {
+ str = "Asia/Rangoon";
+ } else if (tzname.equals("GMT+09:30")) {
+ str = "Australia/Darwin";
+ } else if (tzname.equals("GMT+09:00")) {
+ str = "Asia/Tokyo";
+ }
+ return str;
+ }
+
+ static void updateTimeZone(Context context, String paramString) {
+ AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
+ ICLogger.d("updateTimeZone:" + paramString);
+ if (paramString == null) {
+ ICLogger.d("updateTimeZone name is null");
+ return;
+ }
+ String[] arrayOfString = TimeZone.getAvailableIDs();
+ TimeZone timeZone = TimeZone.getDefault();
+ boolean bool = false;
+ //遍历所有支持的ID,如果字串相同,则设置对应的timezone,否则直接用字符串进行设置
+ for (int b = 0; b < arrayOfString.length; b++) {
+ String tztmp = arrayOfString[b];
+ TimeZone timeZone1 = TimeZone.getTimeZone(tztmp);
+ String tzid = timeZone1.getID();
+ // ICLogger.d(b+"/"+arrayOfString.length+" "+paramString+"== "+tztmp+"? ID="+tzid);
+ if (paramString.equals(timeZone1.getDisplayName(false, TimeZone.SHORT)) && timeZone1.getID().contains("Etc/GMT")) {
+
+ String str1 = timeZone1.getTimeZone(tztmp).getDisplayName();
+ String str2 = TimeZone.getTimeZone(tztmp).getID();
+
+ str1 = "I have find timeZoneDisplayName:" + str1 + " ID:" + str2;
+ ICLogger.d("===tmp:" + str1);
+ ICLogger.d("setTimeZone:" + timeZone1.getDisplayName());
+ alarmManager.setTimeZone(timeZone1.getID());
+ return;
+ }
+ }
+ ICLogger.d(paramString + " not found , set to default timezone " + timeZone.getID());
+ alarmManager.setTimeZone(getTimeZoneID_ByDisplayName(paramString));
+ }
}