All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ymate.platform.commons.util.DateTimeUtils Maven / Gradle / Ivy

There is a newer version: 1.1
Show newest version
/*
 * Copyright 2007-2107 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.commons.util;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 

* DateTimeUtils *

*

* 日期时间数据处理工具类; *

* * @author 刘镇([email protected]) * @version 0.0.0 * * * * * * * * * * * *
版本号动作修改人修改时间
0.0.0创建类刘镇2010-4-18上午02:40:41
*/ public class DateTimeUtils { public static final long SECOND = 1000; // 1秒 public static final long MINUTE = SECOND * 60; // 1分钟 public static final long HOUR = MINUTE * 60; // 1小时 public static final long DAY = HOUR * 24; // 1天 public static final long WEEK = DAY * 7; // 1周 public static final long YEAR = DAY * 365; // 1年(or 366 ???) /** * 日期格式化字符串:yyyy-MM-dd HH:mm:ss.SSS */ public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS"; /** * 日期格式化字符串:yyyy-MM-dd HH:mm:ss */ public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; /** * 日期格式化字符串:yyyy-MM-dd */ public static final String YYYY_MM_DD = "yyyy-MM-dd"; /** * 日期格式化字符串:yyyy-MM */ public static final String YYYY_MM = "yyyy-MM"; /** * 日期格式化字符串:yyyy-MM-dd HH:mm */ public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm"; private static Map __timeZoneIDs = new LinkedHashMap(32); static { __timeZoneIDs.put("-12", new String[]{"GMT-12:00", "(GMT -12:00) Eniwetok, Kwajalein"}); __timeZoneIDs.put("-11", new String[]{"GMT-11:00", "(GMT -11:00) Midway Island, Samoa"}); __timeZoneIDs.put("-10", new String[]{"GMT-10:00", "(GMT -10:00) Hawaii"}); __timeZoneIDs.put("-9", new String[]{"GMT-09:00", "(GMT -09:00) Alaska"}); __timeZoneIDs.put("-8", new String[]{"GMT-08:00", "(GMT -08:00) Pacific Time (US & Canada), Tijuana"}); __timeZoneIDs.put("-7", new String[]{"GMT-07:00", "(GMT -07:00) Mountain Time (US & Canada), Arizona"}); __timeZoneIDs.put("-6", new String[]{"GMT-06:00", "(GMT -06:00) Central Time (US & Canada), Mexico City"}); __timeZoneIDs.put("-5", new String[]{"GMT-05:00", "(GMT -05:00) Eastern Time (US & Canada), Bogota, Lima, Quito"}); __timeZoneIDs.put("-4", new String[]{"GMT-04:00", "(GMT -04:00) Atlantic Time (Canada), Caracas, La Paz"}); __timeZoneIDs.put("-3.5", new String[]{"GMT-03:30", "(GMT -03:30) Newfoundland"}); __timeZoneIDs.put("-3", new String[]{"GMT-03:00", "(GMT -03:00) Brassila, Buenos Aires, Georgetown, Falkland Is"}); __timeZoneIDs.put("-2", new String[]{"GMT-02:00", "(GMT -02:00) Mid-Atlantic, Ascension Is., St. Helena"}); __timeZoneIDs.put("-1", new String[]{"GMT-01:00", "(GMT -01:00) Azores, Cape Verde Islands"}); __timeZoneIDs.put("0", new String[]{"GMT", "(GMT) Casablanca, Dublin, Edinburgh, London, Lisbon, Monrovia"}); __timeZoneIDs.put("1", new String[]{"GMT+01:00", "(GMT +01:00) Amsterdam, Berlin, Brussels, Madrid, Paris, Rome"}); __timeZoneIDs.put("2", new String[]{"GMT+02:00", "(GMT +02:00) Cairo, Helsinki, Kaliningrad, South Africa"}); __timeZoneIDs.put("3", new String[]{"GMT+03:00", "(GMT +03:00) Baghdad, Riyadh, Moscow, Nairobi"}); __timeZoneIDs.put("3.5", new String[]{"GMT+03:30", "(GMT +03:30) Tehran"}); __timeZoneIDs.put("4", new String[]{"GMT+04:00", "(GMT +04:00) Abu Dhabi, Baku, Muscat, Tbilisi"}); __timeZoneIDs.put("4.5", new String[]{"GMT+04:30", "(GMT +04:30) Kabul"}); __timeZoneIDs.put("5", new String[]{"GMT+05:00", "(GMT +05:00) Ekaterinburg, Islamabad, Karachi, Tashkent"}); __timeZoneIDs.put("5.5", new String[]{"GMT+05:30", "(GMT +05:30) Bombay, Calcutta, Madras, New Delhi"}); __timeZoneIDs.put("5.75", new String[]{"GMT+05:45", "(GMT +05:45) Katmandu"}); __timeZoneIDs.put("6", new String[]{"GMT+06:00", "(GMT +06:00) Almaty, Colombo, Dhaka, Novosibirsk"}); __timeZoneIDs.put("6.5", new String[]{"GMT+06:30", "(GMT +06:30) Rangoon"}); __timeZoneIDs.put("7", new String[]{"GMT+07:00", "(GMT +07:00) Bangkok, Hanoi, Jakarta"}); __timeZoneIDs.put("8", new String[]{"GMT+08:00", "(GMT +08:00) Beijing, Hong Kong, Perth, Singapore, Taipei"}); __timeZoneIDs.put("9", new String[]{"GMT+09:00", "(GMT +09:00) Osaka, Sapporo, Seoul, Tokyo, Yakutsk"}); __timeZoneIDs.put("9.5", new String[]{"GMT+09:30", "(GMT +09:30) Adelaide, Darwin"}); __timeZoneIDs.put("10", new String[]{"GMT+10:00", "(GMT +10:00) Canberra, Guam, Melbourne, Sydney, Vladivostok"}); __timeZoneIDs.put("11", new String[]{"GMT+11:00", "(GMT +11:00) Magadan, New Caledonia, Solomon Islands"}); __timeZoneIDs.put("12", new String[]{"GMT+12:00", "(GMT +12:00) Auckland, Wellington, Fiji, Marshall Island"}); } public static Map getTimeZoneIDs() { return __timeZoneIDs; } public static SimpleDateFormat getSimpleDateFormat(String format, String timeoffset) { SimpleDateFormat _format = new SimpleDateFormat(format, Locale.ENGLISH); if (StringUtils.isNotBlank(timeoffset) && __timeZoneIDs.containsKey(timeoffset)) { _format.setTimeZone(TimeZone.getTimeZone(__timeZoneIDs.get(timeoffset)[0])); } return _format; } /** * 时间修正 */ private static String timeOffset; /** * 私有构造器, 防止被实例化 */ private DateTimeUtils() { } /** * 设定时间修正 * * @param timeOffset * @return */ public static void setTimeOffset(String timeOffset) { DateTimeUtils.timeOffset = timeOffset; } /** * 统一的获取当前时间的方法 * * @return */ public static long currentTimeMillis() { return System.currentTimeMillis(); } /** * 获取全局时间的秒数 * * @return */ public static long currentTimeMillisUTC() { return currentTimeMillis() / 1000L; } /** * 获得当前时间 * * @return */ public static Date currentTime() { return new Date(currentTimeMillis()); } /** * 系统当前时间 * * @return */ public static long systemTimeMillis() { return System.currentTimeMillis(); } /** * 系统当前时间 * * @return */ public static Date systemTime() { return new Date(System.currentTimeMillis()); } /** * 获取当前UTC时间 * * @return */ public static int systemTimeUTC() { return (int) (systemTimeMillis() / 1000); } /** * 格式化日期时间输出字符串 * * @param time 日期时间值 * @param pattern 日期时间输出模式,若为空则使用yyyy-MM-dd HH:mm:ss.SSS作为默认 * @return */ public static String formatTime(long time, String pattern) { if (time < 0) { time = 0; } if (StringUtils.isBlank(pattern)) { pattern = YYYY_MM_DD_HH_MM_SS; } return getSimpleDateFormat(pattern, timeOffset).format(new Date(time)); } public static String formatTime(long time, String pattern, String timeoffset) { if (time < 0) { time = 0; } if (StringUtils.isBlank(pattern)) { pattern = YYYY_MM_DD_HH_MM_SS; } return getSimpleDateFormat(pattern, timeoffset).format(new Date(time)); } public static Date parseDateTime(String dateTime, String pattern) throws ParseException { if (StringUtils.isBlank(pattern)) { pattern = YYYY_MM_DD_HH_MM_SS; } return getSimpleDateFormat(pattern, timeOffset).parse(dateTime); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy