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

📄 utildatetime.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            return new java.sql.Time(newDate.getTime());        } else {            return null;        }    }    /**     * Makes a java.sql.Time from separate Strings for hour, minute, and second.     *     * @param hourStr   The hour String     * @param minuteStr The minute String     * @param secondStr The second String     * @return A java.sql.Time made from separate Strings for hour, minute, and second.     */    public static java.sql.Time toSqlTime(String hourStr, String minuteStr, String secondStr) {        java.util.Date newDate = toDate("0", "0", "0", hourStr, minuteStr, secondStr);        if (newDate != null) {            return new java.sql.Time(newDate.getTime());        } else {            return null;        }    }    /**     * Makes a java.sql.Time from separate ints for hour, minute, and second.     *     * @param hour   The hour int     * @param minute The minute int     * @param second The second int     * @return A java.sql.Time made from separate ints for hour, minute, and second.     */    public static java.sql.Time toSqlTime(int hour, int minute, int second) {        java.util.Date newDate = toDate(0, 0, 0, hour, minute, second);        if (newDate != null) {            return new java.sql.Time(newDate.getTime());        } else {            return null;        }    }    /**     * Converts a date and time String into a Timestamp     *     * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional     * @return The corresponding Timestamp     */    public static java.sql.Timestamp toTimestamp(String dateTime) {        java.util.Date newDate = toDate(dateTime);        if (newDate != null) {            return new java.sql.Timestamp(newDate.getTime());        } else {            return null;        }    }    /**     * Converts a date String and a time String into a Timestamp     *     * @param date The date String: MM/DD/YYYY     * @param time The time String: either HH:MM or HH:MM:SS     * @return A Timestamp made from the date and time Strings     */    public static java.sql.Timestamp toTimestamp(String date, String time) {        java.util.Date newDate = toDate(date, time);        if (newDate != null) {            return new java.sql.Timestamp(newDate.getTime());        } else {            return null;        }    }    /**     * Makes a Timestamp from separate Strings for month, day, year, hour, minute, and second.     *     * @param monthStr  The month String     * @param dayStr    The day String     * @param yearStr   The year String     * @param hourStr   The hour String     * @param minuteStr The minute String     * @param secondStr The second String     * @return A Timestamp made from separate Strings for month, day, year, hour, minute, and second.     */    public static java.sql.Timestamp toTimestamp(String monthStr, String dayStr, String yearStr, String hourStr,            String minuteStr, String secondStr) {        java.util.Date newDate = toDate(monthStr, dayStr, yearStr, hourStr, minuteStr, secondStr);        if (newDate != null) {            return new java.sql.Timestamp(newDate.getTime());        } else {            return null;        }    }    /**     * Makes a Timestamp from separate ints for month, day, year, hour, minute, and second.     *     * @param month  The month int     * @param day    The day int     * @param year   The year int     * @param hour   The hour int     * @param minute The minute int     * @param second The second int     * @return A Timestamp made from separate ints for month, day, year, hour, minute, and second.     */    public static java.sql.Timestamp toTimestamp(int month, int day, int year, int hour, int minute, int second) {        java.util.Date newDate = toDate(month, day, year, hour, minute, second);        if (newDate != null) {            return new java.sql.Timestamp(newDate.getTime());        } else {            return null;        }    }    public static java.sql.Timestamp toTimestamp(Date date) {        if (date == null) return null;        return new Timestamp(date.getTime());    }    /**     * Converts a date and time String into a Date     *     * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional     * @return The corresponding Date     */    public static java.util.Date toDate(String dateTime) {        if (dateTime == null) {            return null;        }        // dateTime must have one space between the date and time...        String date = dateTime.substring(0, dateTime.indexOf(" "));        String time = dateTime.substring(dateTime.indexOf(" ") + 1);        return toDate(date, time);    }    /**     * Converts a date String and a time String into a Date     *     * @param date The date String: MM/DD/YYYY     * @param time The time String: either HH:MM or HH:MM:SS     * @return A Date made from the date and time Strings     */    public static java.util.Date toDate(String date, String time) {        if (date == null || time == null) return null;        String month;        String day;        String year;        String hour;        String minute;        String second;        int dateSlash1 = date.indexOf("/");        int dateSlash2 = date.lastIndexOf("/");        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return null;        int timeColon1 = time.indexOf(":");        int timeColon2 = time.lastIndexOf(":");        if (timeColon1 <= 0) return null;        month = date.substring(0, dateSlash1);        day = date.substring(dateSlash1 + 1, dateSlash2);        year = date.substring(dateSlash2 + 1);        hour = time.substring(0, timeColon1);        if (timeColon1 == timeColon2) {            minute = time.substring(timeColon1 + 1);            second = "0";        } else {            minute = time.substring(timeColon1 + 1, timeColon2);            second = time.substring(timeColon2 + 1);        }        return toDate(month, day, year, hour, minute, second);    }    /**     * Makes a Date from separate Strings for month, day, year, hour, minute, and second.     *     * @param monthStr  The month String     * @param dayStr    The day String     * @param yearStr   The year String     * @param hourStr   The hour String     * @param minuteStr The minute String     * @param secondStr The second String     * @return A Date made from separate Strings for month, day, year, hour, minute, and second.     */    public static java.util.Date toDate(String monthStr, String dayStr, String yearStr, String hourStr,            String minuteStr, String secondStr) {        int month, day, year, hour, minute, second;        try {            month = Integer.parseInt(monthStr);            day = Integer.parseInt(dayStr);            year = Integer.parseInt(yearStr);            hour = Integer.parseInt(hourStr);            minute = Integer.parseInt(minuteStr);            second = Integer.parseInt(secondStr);        } catch (Exception e) {            return null;        }        return toDate(month, day, year, hour, minute, second);    }    /**     * Makes a Date from separate ints for month, day, year, hour, minute, and second.     *     * @param month  The month int     * @param day    The day int     * @param year   The year int     * @param hour   The hour int     * @param minute The minute int     * @param second The second int     * @return A Date made from separate ints for month, day, year, hour, minute, and second.     */    public static java.util.Date toDate(int month, int day, int year, int hour, int minute, int second) {        Calendar calendar = Calendar.getInstance();        try {            calendar.set(year, month - 1, day, hour, minute, second);        } catch (Exception e) {            return null;        }        return new java.util.Date(calendar.getTime().getTime());    }    /**     * Makes a date String in the given from a Date     *     * @param date The Date     * @return A date String in the given format     */    public static String toDateString(java.util.Date date, String format) {        if (date == null) return "";        SimpleDateFormat dateFormat = null;        if (format != null) {            dateFormat = new SimpleDateFormat(format);        } else {            dateFormat = new SimpleDateFormat();        }        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        return dateFormat.format(date);    }    /**     * Makes a date String in the format MM/DD/YYYY from a Date     *     * @param date The Date     * @return A date String in the format MM/DD/YYYY     */    public static String toDateString(java.util.Date date) {        return toDateString(date, "MM/dd/yyyy");    }    /**     * Makes a time String in the format HH:MM:SS from a Date. If the seconds are 0, then the output is in HH:MM.     *     * @param date The Date     * @return A time String in the format HH:MM:SS or HH:MM     */    public static String toTimeString(java.util.Date date) {        if (date == null) return "";        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        return (toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)));    }    /**     * Makes a time String in the format HH:MM:SS from a separate ints for hour, minute, and second. If the seconds are 0, then the output is in HH:MM.     *     * @param hour   The hour int     * @param minute The minute int     * @param second The second int     * @return A time String in the format HH:MM:SS or HH:MM     */    public static String toTimeString(int hour, int minute, int second) {        String hourStr;        String minuteStr;        String secondStr;        if (hour < 10) {            hourStr = "0" + hour;        } else {            hourStr = "" + hour;        }        if (minute < 10) {            minuteStr = "0" + minute;        } else {            minuteStr = "" + minute;        }        if (second < 10) {            secondStr = "0" + second;        } else {            secondStr = "" + second;        }        if (second == 0) {            return hourStr + ":" + minuteStr;        } else {            return hourStr + ":" + minuteStr + ":" + secondStr;        }    }    /**     * Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS" from a Date. If the seconds are 0 they are left off.     *     * @param date The Date     * @return A combined data and time string in the format "MM/DD/YYYY HH:MM:SS" where the seconds are left off if they are 0.     */    public static String toDateTimeString(java.util.Date date) {        if (date == null) return "";        String dateString = toDateString(date);        String timeString = toTimeString(date);        if (dateString != null && timeString != null) {            return dateString + " " + timeString;        } else {            return "";        }    }    /**     * Makes a Timestamp for the beginning of the month     *     * @return A Timestamp of the beginning of the month     */    public static java.sql.Timestamp monthBegin() {        Calendar mth = Calendar.getInstance();        mth.set(Calendar.DAY_OF_MONTH, 1);        mth.set(Calendar.HOUR_OF_DAY, 0);        mth.set(Calendar.MINUTE, 0);        mth.set(Calendar.SECOND, 0);        mth.set(Calendar.MILLISECOND, 0);        mth.set(Calendar.AM_PM, Calendar.AM);        return new java.sql.Timestamp(mth.getTime().getTime());    }    /**     * returns a week number in a year for a Timestamp input     *     * @param input Timestamp date     * @return A int containing the week number     */    public static int weekNumber(Timestamp input) {        Timestamp yearStart = UtilDateTime.getYearStart(input);        Timestamp weekStart = UtilDateTime.getWeekStart(yearStart);        int days = 0;        for (days = 0; UtilDateTime.getDayStart(weekStart, days).compareTo(yearStart) == 0; days++) ;        // the splitted week belongs to the year where there are the most days (ISO)        Timestamp week1Start = weekStart;        if (days < 4)            week1Start = UtilDateTime.getWeekStart(weekStart, 7);        int weeks = 0;        for (weeks = 0; UtilDateTime.getDayStart(week1Start, weeks * 7).compareTo(input) < 0; weeks++) ;        return ++weeks; // start at 1    }}

⌨️ 快捷键说明

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