设置系统默认时区 | TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); TimeZone.setDefault(tz); |
获取系统默认时区 | String str = TimeZone.getDefault().getDisplayName(); Log.e("gmx",str); |
日期计算过程中 临时时区 | final Calendar now = Calendar.getInstance();
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
now.setTimeZone(tz); |
Date2String | public static String date2String(java.util.Date date, String pattern) { SimpleDateFormat formatter = new SimpleDateFormat(pattern); String dateString = formatter.format(date); return dateString; } |
Data2Timestamp | public static int date2TimeStamp(Date time) { return (int)(time.getTime()/1000); } |
Timestamp2String | public static String timeStamp2String(long timeStamp) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(timeStamp * 1000); return dateString; }
public static String timeStamp2String(long timeStamp, String pattern) { SimpleDateFormat formatter = new SimpleDateFormat(pattern); String dateString = formatter.format(timeStamp * 1000); return dateString; } |
String2Date | public static Date string2Date(String timeStr,String pattern) { SimpleDateFormat formatter = new SimpleDateFormat(pattern); //formatter.setTimeZone(TimeZone.getTimeZone("GMT")); ParsePosition pos = new ParsePosition(0); Date result = formatter.parse(timeStr, pos); return result; } (红色标识处表示使用自定义TimeZone而不使用系统默认,该函数可用于其它转换方法(其它方法省略使用系统默认)) |
String2Timestamp | public static int string2Timestamp(String timeStr, String pattern) { Date time = string2Date(timeStr,pattern); return (int)(time.getTime()/1000); } |
get now date/time/hours minutes/seconds string ... | public static String getNowString(String pattern) { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat(pattern); String dateString = formatter.format(currentTime); return dateString; } |
pattern: such as yyyy-MM-dd HH:mm:ss/yyyy-MM-dd/HH:mm:ss/mm:ss/mm/ss/HH ..
备注:以上转换均需设置为需要使用的区时。