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

📄 timeutility.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.misc.util;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * <p>Title:时间简使</p>
 * <p>Description:用于格式化日期的所有通用处理函数</p>
 * <p>Copyright: Copyright (c) 2004-1-8</p>
 * <p>Company: </p>
 *
 * @version 1.0
 * @author Eric.Li
 *
 */
public class TimeUtility {
    public static String DATE = "date";
    public static String HOUR = "hour";
    public static String SEC = "sec";
    /**
     * 将Date型日期转化成某种格式的字符串
     * 
     * @param date 日期
     * @param format 转换格式 例如:"yyyy-MM-dd"
     * @return 指定格式字符串
     */
    public static String getFormatTime(Date date, String format) {
        if (date == null)
            return "";
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date).toString();
    }
    /**
     * 将Long型日期转换成相应格式的字符串
     * 
     * @param sec 时间对应的秒数
     * @param format 转换的格式 例如:"yyyy-MM-dd"
     * @return 指定格式的字符串
     */
    public static String getFormatTime(Long sec, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date date = new Date(sec.longValue());
        return formatter.format(date).toString();
    }
    /**
     * 得到指定格式的当前日期
     * 
     * @param format 指定的日期格式 例如:"yyyy-MM-dd"
     * @return 指定格式的日期
     */
    public static String getCurrentFormatTime(String format) {
        if (format.equals(HOUR))
            return getFormatTime(new Long(System.currentTimeMillis()), "yyyy-MM-dd HH:mm");
        if (format.equals(SEC))
            return getFormatTime(new Long(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss");
        else
            return getFormatTime(new Long(System.currentTimeMillis()), "yyyy-MM-dd");
    }
    /**
     * 得到当前时间之后几天后的时间,秒记录
     * 
     * @param date 天数
     * @return 返回几天之后的时间
     */
    public static Long getRemindDay(int date) {
        Date now = new Date();
        long t = now.getTime();
        long theDay = t + date * 24 * 60 * 60 * 1000;
        return new Long(theDay);
    }
    /**
     * 得到特定某天后时间
     * 
     * @param s
     * @param date
     * @return
     */
    public static Long getRemindDay(String s, int date) {
        long t = str_To_TimeSec(s, "yyyy-MM-dd").longValue();
        long theDay = t + date * 24 * 60 * 60 * 1000;
		return new Long(theDay);
    }
    /**
     * 将指定格式的日期型字符串转换成相应的秒数
     * 
     * @param s 时间型字符串
     * @param format 字符串的格式 例如:"yyyy-MM-dd HH:mm:ss"
     * @return 秒数
     */
    public static Long str_To_TimeSec(String s, String format) {
        if (s == null || "".equals(s.trim()))
            return new Long(System.currentTimeMillis());
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            Date d = formatter.parse(s);
            return new Long(d.getTime());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Long(System.currentTimeMillis());
        }
    }
    public TimeUtility() {}
    public static void main(String[] args) {}
}

⌨️ 快捷键说明

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