Java tutorial
/* * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los trminos * de la licencia "GNU General Public License" publicada por la Fundacin "Free Software Foundation". * Este programa se distribuye con la esperanza de que pueda ser til, pero SIN NINGUNA GARANTIA; * vea la licencia "GNU General Public License" para obtener mas informacin. */ package ips1ap101.lib.base.util; import java.sql.Date; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; import ips1ap101.lib.base.bundle.BundleWebui; import org.apache.commons.lang.StringUtils; import org.joda.time.DateTime; /** * @author Jorge Campins */ public class TimeUtils { private static long currentTimeMillis = 0; private static long lastTimeMillis = 0; private static long lastTimeMicros = 0; private static long lastTimeNanos = 0; private static long micros = 0; private static long nanos = 0; private static Calendar calendar = Calendar.getInstance(); public static synchronized long currentTimeMillis() { currentTimeMillis = System.currentTimeMillis(); if (lastTimeMillis < currentTimeMillis) { lastTimeMillis = currentTimeMillis; } else { lastTimeMillis++; } lastTimeMicros = lastTimeMillis * 1000; lastTimeNanos = lastTimeMicros * 1000; micros = 0; nanos = 0; return lastTimeMillis; } public static synchronized long currentTimeMicros() { currentTimeMillis = System.currentTimeMillis(); if (lastTimeMillis < currentTimeMillis) { currentTimeMillis(); } else { if (++micros < 1000) { lastTimeMicros++; lastTimeNanos = lastTimeMicros * 1000; nanos = 0; } else { currentTimeMillis(); } } return lastTimeMicros; } public static synchronized long currentTimeNanos() { currentTimeMillis = System.currentTimeMillis(); if (lastTimeMillis < currentTimeMillis) { currentTimeMillis(); } else { if (++nanos < 1000) { lastTimeNanos++; } else { currentTimeMicros(); } } return lastTimeNanos; } public static synchronized Date currentDate() { calendar.setTimeInMillis(currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return new Date(calendar.getTimeInMillis()); } public static synchronized Time currentTime() { calendar.setTimeInMillis(currentTimeMillis()); calendar.set(Calendar.YEAR, 1970); calendar.set(Calendar.MONTH, Calendar.JANUARY); calendar.set(Calendar.DAY_OF_MONTH, 1); return new Time(calendar.getTimeInMillis()); } public static synchronized Timestamp currentTimestamp() { return new Timestamp(currentTimeMillis()); } public static synchronized Date getDate() { return currentDate(); } public static synchronized Date getDate(java.util.Date date) { if (date == null) { return currentDate(); } Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return new Date(c.getTimeInMillis()); } public static synchronized Time getTime() { return currentTime(); } public static synchronized Time getTime(java.util.Date date) { if (date == null) { return currentTime(); } Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); c.set(Calendar.YEAR, 1970); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DAY_OF_MONTH, 1); return new Time(c.getTimeInMillis()); } public static synchronized Timestamp getTimestamp() { return currentTimestamp(); } public static synchronized Timestamp getTimestamp(Date date) { return date == null ? currentTimestamp() : new Timestamp(date.getTime()); } public static final String DEFAULT_DATE_FORMAT = "dd/MM/yyyy"; public static final String DEFAULT_TIME_FORMAT = "hh:mm aa"; public static final String DEFAULT_TIMESTAMP_FORMAT = "dd/MM/yyyy hh:mm:ss aa"; public static String getDateFormat() { String string = BundleWebui.getTrimmedToNullString("DateTimeConverter.date.pattern"); String format = StringUtils.defaultIfBlank(string, DEFAULT_DATE_FORMAT); return format; } public static String getTimeFormat() { String string = BundleWebui.getTrimmedToNullString("DateTimeConverter.time.pattern"); String format = StringUtils.defaultIfBlank(string, DEFAULT_TIME_FORMAT); return format; } public static String getTimestampFormat() { String string = BundleWebui.getTrimmedToNullString("DateTimeConverter.both.pattern"); String format = StringUtils.defaultIfBlank(string, DEFAULT_TIMESTAMP_FORMAT); return format; } public static String defaultDateString() { return defaultDateString(currentDate()); } public static String defaultDateString(Object object) { java.util.Date date = object instanceof java.util.Date ? (java.util.Date) object : null; return date == null ? null : defaultDateString(date); } public static String defaultDateString(java.util.Date date) { return date == null ? null : new DateTime(date).toString(getDateFormat()); } public static String defaultTimeString() { return defaultTimeString(currentTime()); } public static String defaultTimeString(Object object) { java.util.Date date = object instanceof java.util.Date ? (java.util.Date) object : null; return date == null ? null : defaultTimeString(date); } public static String defaultTimeString(java.util.Date date) { return date == null ? null : new DateTime(date).toString(getTimeFormat()); } public static String defaultTimestampString() { return defaultTimestampString(currentTimestamp()); } public static String defaultTimestampString(Object object) { java.util.Date date = object instanceof java.util.Date ? (java.util.Date) object : null; return date == null ? null : defaultTimestampString(date); } public static String defaultTimestampString(java.util.Date date) { return date == null ? null : new DateTime(date).toString(getTimestampFormat()); } public static final String JDBC_DATE_FORMAT = "yyyy-MM-dd"; public static final String JDBC_TIME_FORMAT = "HH:mm:ss"; public static final String JDBC_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static String jdbcDateString(Object object) { java.util.Date date = object instanceof java.util.Date ? (java.util.Date) object : null; return date == null ? null : jdbcDateString(date); } public static String jdbcDateString(java.util.Date date) { return date == null ? null : new DateTime(date).toString(JDBC_DATE_FORMAT); } public static String jdbcTimeString(Object object) { java.util.Date date = object instanceof java.util.Date ? (java.util.Date) object : null; return date == null ? null : jdbcTimeString(date); } public static String jdbcTimeString(java.util.Date date) { return date == null ? null : new DateTime(date).toString(JDBC_TIME_FORMAT); } public static String jdbcTimestampString(Object object) { java.util.Date date = object instanceof java.util.Date ? (java.util.Date) object : null; return date == null ? null : jdbcTimestampString(date); } public static String jdbcTimestampString(java.util.Date date) { return date == null ? null : new DateTime(date).toString(JDBC_TIMESTAMP_FORMAT); } public static Date newDate(Object obj) { if (obj == null) { return null; } else if (obj instanceof String) { String pdq = (String) obj; return newDate(parse(pdq)); } else if (obj instanceof java.util.Date) { java.util.Date pdq = (java.util.Date) obj; return newDate(pdq); } else { throw new IllegalArgumentException( "(" + obj.getClass() + ")" + obj + " is not an instance of java.util.Date "); } } public static Time newTime(Object obj) { if (obj == null) { return null; } else if (obj instanceof String) { String pdq = (String) obj; return newTime(parse(pdq)); } else if (obj instanceof java.util.Date) { java.util.Date pdq = (java.util.Date) obj; return newTime(pdq); } else { throw new IllegalArgumentException( "(" + obj.getClass() + ")" + obj + " is not an instance of java.util.Date "); } } public static Timestamp newTimestamp(Object obj) { if (obj == null) { return null; } else if (obj instanceof String) { String pdq = (String) obj; return newTimestamp(parse(pdq)); } else if (obj instanceof java.util.Date) { java.util.Date pdq = (java.util.Date) obj; return newTimestamp(pdq); } else { throw new IllegalArgumentException( "(" + obj.getClass() + ")" + obj + " is not an instance of java.util.Date "); } } public static Date newDate(java.util.Date date) { if (date == null) { return null; } else { Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return new Date(c.getTimeInMillis()); } } public static Time newTime(java.util.Date date) { if (date == null) { return null; } else { Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); c.set(Calendar.YEAR, 1970); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DAY_OF_MONTH, 1); return new Time(c.getTimeInMillis()); } } public static Timestamp newTimestamp(java.util.Date date) { if (date == null) { return null; } else { return new Timestamp(date.getTime()); } } public static Date toDate(Object obj) { return obj instanceof Date ? (Date) obj : newDate(obj); } public static Time toTime(Object obj) { return obj instanceof Time ? (Time) obj : newTime(obj); } public static Timestamp toTimestamp(Object obj) { return obj instanceof Timestamp ? (Timestamp) obj : newTimestamp(obj); } public static java.util.Date parse(String pdq) { String string = pdq == null ? null : pdq.toString().trim(); if (string == null || string.isEmpty()) { return null; } int year = 1970; int monthOfYear = 1; int dayOfMonth = 1; int hourOfDay = 0; int minuteOfHour = 0; int secondOfMinute = 0; String xm = ""; String xs = ""; String format = getDateFormat(); int len = string.length(); switch (len) { case 22: xm = string.substring(20); case 19: if (xm.isEmpty()) { xs = string.substring(17, 19); } if (xs.equalsIgnoreCase("AM") || xs.equalsIgnoreCase("PM")) { xm = xs; } else { secondOfMinute = Integer.parseInt(string.substring(17, 19)); } case 16: minuteOfHour = Integer.parseInt(string.substring(14, 16)); hourOfDay = Integer.parseInt(string.substring(11, 13)); if (xm.equalsIgnoreCase("AM") && hourOfDay == 12) { hourOfDay = 0; } if (xm.equalsIgnoreCase("PM") && hourOfDay <= 11) { hourOfDay += 12; } format = getTimestampFormat(); case 10: switch (format.substring(0, 2)) { case "yy": year = Integer.parseInt(string.substring(0, 4)); monthOfYear = Integer.parseInt(string.substring(5, 7)); dayOfMonth = Integer.parseInt(string.substring(8, 10)); break; case "MM": year = Integer.parseInt(string.substring(6, 10)); monthOfYear = Integer.parseInt(string.substring(0, 2)); dayOfMonth = Integer.parseInt(string.substring(3, 5)); break; case "dd": default: year = Integer.parseInt(string.substring(6, 10)); monthOfYear = Integer.parseInt(string.substring(3, 5)); dayOfMonth = Integer.parseInt(string.substring(0, 2)); break; } break; case 8: xm = string.substring(6); case 6: minuteOfHour = Integer.parseInt(string.substring(3, 5)); hourOfDay = Integer.parseInt(string.substring(0, 2)); if (xm.equalsIgnoreCase("AM") && hourOfDay == 12) { hourOfDay = 0; } if (xm.equalsIgnoreCase("PM") && hourOfDay <= 11) { hourOfDay += 12; } break; default: return null; } Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, monthOfYear - 1); c.set(Calendar.DAY_OF_MONTH, dayOfMonth); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minuteOfHour); c.set(Calendar.SECOND, secondOfMinute); c.set(Calendar.MILLISECOND, 0); return new Date(c.getTimeInMillis()); } private static final Calendar EPOCH = Calendar.getInstance(); static { EPOCH.set(Calendar.YEAR, 1970); EPOCH.set(Calendar.MONTH, Calendar.JANUARY); EPOCH.set(Calendar.DAY_OF_MONTH, 1); EPOCH.set(Calendar.HOUR_OF_DAY, 0); EPOCH.set(Calendar.MINUTE, 0); EPOCH.set(Calendar.SECOND, 0); EPOCH.set(Calendar.MILLISECOND, 0); } public static long epochMillis() { return EPOCH.getTimeInMillis(); } } /* TimeUtils */