⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utildatetime.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Id: UtilDateTime.java 6520 2006-01-17 17:02:40Z jaz $ * *  Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * *  Permission is hereby granted, free of charge, to any person obtaining a *  copy of this software and associated documentation files (the "Software"), *  to deal in the Software without restriction, including without limitation *  the rights to use, copy, modify, merge, publish, distribute, sublicense, *  and/or sell copies of the Software, and to permit persons to whom the *  Software is furnished to do so, subject to the following conditions: * *  The above copyright notice and this permission notice shall be included *  in all copies or substantial portions of the Software. * *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *  THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.base.util;import java.sql.Timestamp;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.Locale;import java.util.Map;/** * Utility class for handling java.util.Date, the java.sql data/time classes and related * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author <a href="mailto:johan@ibibi.com">Johan Isacsson</a> * @version $Rev: 6520 $ * @since 2.0 */public class UtilDateTime {    public static final String[] months = {// // to be translated over CommonMonthName, see example in accounting        "January", "February", "March", "April", "May", "June",        "July", "August", "September", "October", "November",        "December"    };    public static final String[] days = {// to be translated over CommonDayName, see example in accounting        "Monday", "Tuesday", "Wednesday",        "Thursday", "Friday", "Saturday", "Sunday"    };    public static final String[][] timevals = {        {"1000", "millisecond"},        {"60", "second"},        {"60", "minute"},        {"24", "hour"},        {"168", "week"}    };    public static final DecimalFormat df = new DecimalFormat("0.00;-0.00");    public static double getInterval(Date from, Date thru) {        return thru != null ? thru.getTime() - from.getTime() : 0;    }    public static double getInterval(Timestamp from, Timestamp thru) {        return thru != null ? thru.getTime() - from.getTime() + (thru.getNanos() - from.getNanos()) / 1000000 : 0;    }    public static String formatInterval(Date from, Date thru, int count, Locale locale) {        return formatInterval(getInterval(from, thru), count, locale);    }    public static String formatInterval(Date from, Date thru, Locale locale) {        return formatInterval(from, thru, 2, locale);    }    public static String formatInterval(Timestamp from, Timestamp thru, int count, Locale locale) {        return formatInterval(getInterval(from, thru), count, locale);    }    public static String formatInterval(Timestamp from, Timestamp thru, Locale locale) {        return formatInterval(from, thru, 2, locale);    }    public static String formatInterval(long interval, int count, Locale locale) {        return formatInterval((double) interval, count, locale);    }    public static String formatInterval(long interval, Locale locale) {        return formatInterval(interval, 2, locale);    }    public static String formatInterval(double interval, Locale locale) {        return formatInterval(interval, 2, locale);    }    public static String formatInterval(double interval, int count, Locale locale) {        ArrayList parts = new ArrayList(timevals.length);        for (int i = 0; i < timevals.length; i++) {            int value = Integer.parseInt(timevals[i][0]);            double remainder = interval % value;            interval = interval / value;            parts.add(new Double(remainder));        }        Map uiDateTimeMap = UtilProperties.getResourceBundleMap("DateTimeLabels", locale);        StringBuffer sb = new StringBuffer();        for (int i = parts.size() - 1; i >= 0 && count > 0; i--) {            if (sb.length() > 0) sb.append(", ");            Double D = (Double) parts.get(i);            double d = D.doubleValue();            if (d < 1) continue;            count--;            sb.append(count == 0 ? df.format(d) : Integer.toString(D.intValue()));            sb.append(' ');            String label;            if (D.intValue() == 1) {                label = (String) uiDateTimeMap.get(timevals[i][1] + ".singular");            } else {                label = (String) uiDateTimeMap.get(timevals[i][1] + ".plural");            }            sb.append(label);        }        return sb.toString();    }    /**     * Return a Timestamp for right now     *     * @return Timestamp for right now     */    public static java.sql.Timestamp nowTimestamp() {        return getTimestamp(System.currentTimeMillis());    }    public static java.sql.Timestamp getTimestamp(long time) {        return new java.sql.Timestamp(time);    }    /**     * Return a string formatted as yyyyMMddHHmmss     *     * @return String formatted for right now     */    public static String nowDateString() {        return nowDateString("yyyyMMddHHmmss");    }    /**     * Return a string formatted as format     *     * @return String formatted for right now     */    public static String nowDateString(String format) {        SimpleDateFormat df = new SimpleDateFormat(format);        return df.format(new Date());    }    /**     * Return a Date for right now     *     * @return Date for right now     */    public static java.util.Date nowDate() {        return new java.util.Date();    }    public static java.sql.Timestamp getDayStart(java.sql.Timestamp stamp) {        return getDayStart(stamp, 0);    }    public static java.sql.Timestamp getDayStart(java.sql.Timestamp stamp, int daysLater) {        Calendar tempCal = Calendar.getInstance();        tempCal.setTime(new java.util.Date(stamp.getTime()));        tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);        tempCal.add(Calendar.DAY_OF_MONTH, daysLater);        java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime());        retStamp.setNanos(0);        return retStamp;    }    public static java.sql.Timestamp getNextDayStart(java.sql.Timestamp stamp) {        return getDayStart(stamp, 1);    }    public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp) {        return getDayEnd(stamp, 0);    }    public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp, int daysLater) {        Calendar tempCal = Calendar.getInstance();        tempCal.setTime(new java.util.Date(stamp.getTime()));        tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);        tempCal.add(Calendar.DAY_OF_MONTH, daysLater);        java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime());        retStamp.setNanos(999999999);        return retStamp;    }    /**     * Return the date for the first day of the year     *     * @param stamp     * @return java.sql.Timestamp     */    public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp) {        return getYearStart(stamp, 0, 0, 0);    }    public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, int daysLater) {        return getYearStart(stamp, daysLater, 0, 0);    }    public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, int daysLater, int yearsLater) {        return getYearStart(stamp, daysLater, 0, yearsLater);    }    public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, int daysLater, int monthsLater, int yearsLater) {        Calendar tempCal = Calendar.getInstance();        tempCal.setTime(new java.util.Date(stamp.getTime()));        tempCal.set(tempCal.get(Calendar.YEAR), Calendar.JANUARY, 1, 0, 0, 0);        tempCal.add(Calendar.YEAR, yearsLater);        tempCal.add(Calendar.MONTH, monthsLater);        tempCal.add(Calendar.DAY_OF_MONTH, daysLater);        java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime());        retStamp.setNanos(0);        return retStamp;    }    public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, Number daysLater, Number monthsLater, Number yearsLater) {        return getYearStart(stamp, (daysLater == null ? 0 : daysLater.intValue()),                 (monthsLater == null ? 0 : monthsLater.intValue()), (yearsLater == null ? 0 : yearsLater.intValue()));    }    /**     * Return the date for the first day of the month     *     * @param stamp     * @return java.sql.Timestamp     */    public static java.sql.Timestamp getMonthStart(java.sql.Timestamp stamp) {        return getMonthStart(stamp, 0, 0);    }    public static java.sql.Timestamp getMonthStart(java.sql.Timestamp stamp, int daysLater) {        return getMonthStart(stamp, daysLater, 0);    }    public static java.sql.Timestamp getMonthStart(java.sql.Timestamp stamp, int daysLater, int monthsLater) {        Calendar tempCal = Calendar.getInstance();        tempCal.setTime(new java.util.Date(stamp.getTime()));        tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), 1, 0, 0, 0);        tempCal.add(Calendar.DAY_OF_MONTH, daysLater);        tempCal.add(Calendar.MONTH, monthsLater);        java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime());        retStamp.setNanos(0);        return retStamp;    }    /**     * Return the date for the first day of the week     *     * @param stamp     * @return java.sql.Timestamp     */    public static java.sql.Timestamp getWeekStart(java.sql.Timestamp stamp) {        return getWeekStart(stamp, 0, 0);    }    public static java.sql.Timestamp getWeekStart(java.sql.Timestamp stamp, int daysLater) {        return getWeekStart(stamp, daysLater, 0);    }    public static java.sql.Timestamp getWeekStart(java.sql.Timestamp stamp, int daysLater, int weeksLater) {        Calendar tempCal = Calendar.getInstance();        tempCal.setTime(new java.util.Date(stamp.getTime()));        tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);        tempCal.add(Calendar.DAY_OF_MONTH, daysLater);        tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek());        tempCal.add(Calendar.WEEK_OF_MONTH, weeksLater);        java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime());        retStamp.setNanos(0);        return retStamp;    }    public static java.sql.Timestamp getWeekEnd(java.sql.Timestamp stamp) {        Calendar tempCal = Calendar.getInstance();        tempCal.setTime(new java.util.Date(stamp.getTime()));        tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);        tempCal.add(Calendar.WEEK_OF_MONTH, 1);        tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek());        tempCal.add(Calendar.SECOND, -1);        java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal.getTime().getTime());        retStamp.setNanos(0);        return retStamp;    }        public static java.util.Calendar toCalendar(java.sql.Timestamp stamp) {        Calendar cal = Calendar.getInstance();        if (stamp != null) {            cal.setTimeInMillis(stamp.getTime());        }        return cal;    }    /**     * Converts a date String into a java.sql.Date     *     * @param date The date String: MM/DD/YYYY     * @return A java.sql.Date made from the date String     */    public static java.sql.Date toSqlDate(String date) {        java.util.Date newDate = toDate(date, "00:00:00");        if (newDate != null) {            return new java.sql.Date(newDate.getTime());        } else {            return null;        }    }    /**     * Makes a java.sql.Date from separate Strings for month, day, year     *     * @param monthStr The month String     * @param dayStr   The day String     * @param yearStr  The year String     * @return A java.sql.Date made from separate Strings for month, day, year     */    public static java.sql.Date toSqlDate(String monthStr, String dayStr, String yearStr) {        java.util.Date newDate = toDate(monthStr, dayStr, yearStr, "0", "0", "0");        if (newDate != null) {            return new java.sql.Date(newDate.getTime());        } else {            return null;        }    }    /**     * Makes a java.sql.Date from separate ints for month, day, year     *     * @param month The month int     * @param day   The day int     * @param year  The year int     * @return A java.sql.Date made from separate ints for month, day, year     */    public static java.sql.Date toSqlDate(int month, int day, int year) {        java.util.Date newDate = toDate(month, day, year, 0, 0, 0);        if (newDate != null) {            return new java.sql.Date(newDate.getTime());        } else {            return null;        }    }    /**     * Converts a time String into a java.sql.Time     *     * @param time The time String: either HH:MM or HH:MM:SS     * @return A java.sql.Time made from the time String     */    public static java.sql.Time toSqlTime(String time) {        java.util.Date newDate = toDate("1/1/1970", time);        if (newDate != null) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -