您的位置:首页 > 编程语言 > Java开发

日期工具类

2015-11-26 11:30 645 查看
package glodon.td.utils;

 

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

 

/**

 * 日期处理相关工具类

 * @author sunhj-c

 *

 */

public class DateUtils {

     

    /**定义常量**/

    public static final String DATE_JFP_STR="yyyyMM";

    public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FULL_STR_SSS = "yyyy-MM-dd HH:mm:ss SSS";

    public static final String DATE_SMALL_STR = "yyyy-MM-dd";

    public static final String DATE_KEY_STR = "yyMMddHHmmss";

    public static final String DATE_KEY_STR_SSS = "yyyyMMddHHmmssSSS";

     

    /**

     * 使用预设格式提取字符串日期

     * @param strDate 日期字符串

     * @return

     */

    public static Date parse(String strDate) {

        return parse(strDate,DATE_FULL_STR);

    }

     

    /**

     * 使用用户格式提取字符串日期

     * @param strDate 日期字符串

     * @param pattern 日期格式

     * @return

     */

    public static Date parse(String strDate, String pattern) {

        SimpleDateFormat df = new SimpleDateFormat(pattern);

        try {

            return df.parse(strDate);

        } catch (ParseException e) {

            e.printStackTrace();

            return null;

        }

    }

    

    

    /**

     * 将LONG时间转化为Date

     * @param times

     * @return

     */
public static String parseLongToDate(long times) {
// 计算毫秒
long mm = times % 1000;

        long fg=times*1;
if (fg < 0) {
mm = 0;
return "" + mm + "天" + mm + "时" + mm + "分" + mm + "秒";

// return "" + mm + "天" + mm + "时" + mm + "分" + mm + "秒" + mm + "毫秒";
}

//秒
long x = (times - mm) / 1000;

//计算秒数
long s = x % 60;

//分钟
x = (x - s)/60;
long mi = x % 60;

//小时
x = (x - mi) / 60;
long h = x % 24;

// 天
x = (x - h) / 24;
long d = x;

String tString = "" + d + "天" + h + "时" + mi + "分" + s + "秒";

// String tString = "" + d + "天" + h + "时" + mi + "分" + s + "秒" + mm

// + "毫秒";
return tString;
}

   

    /**

     * 获取系统当前时间

     * @return

     */

    public static String getNowTime(String type) {

        SimpleDateFormat df = new SimpleDateFormat(type);

        return df.format(new Date());

    }

    

    

    /**

     * 格式化指定时间

     * @return

     */

    public static String getNowTime(String type,Date time) {

        SimpleDateFormat df = new SimpleDateFormat(type);

        return df.format(time);

    }

    

    

    /**

     * 获取系统当前时间

     * @return

     */

    public static String getDateTime(Date time,String type) {

        SimpleDateFormat df = new SimpleDateFormat(type);

        return df.format(time);

    }

    

    /**

     * 获取系统当前时间

     * @return

     */

    public static Date getNowTime() {

        return new Date();

    }

    

    /**

     * 延迟某个时间

     * @param begin

     * @param delayType

     * @param number

     * @return

     */

    public static Date getDelayTime(Date beginTime,int delayType,int number) 

    {

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(beginTime);

        calendar.add(delayType, number);

        return calendar.getTime(); 

    }

    

    
 /**

     * 将时间转换成字符串

     * @param date 日期

     * @return

     */

    public static String getDateString(Date date) {

    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FULL_STR);

        return  dateFormat.format(date);

    }

    

    public static void main(String[] args) {

    String s=parseLongToDate(-10000);

    System.out.println(s);
}

 

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  日期 java