java時間類簡單整合

來源:互聯網
上載者:User

標籤:

package com.rengh.utils;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;public class DateUtils {    /** 格式化時間 */    public static String formatTime(long showTime) {        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Long                .valueOf(showTime));    }    /** 格式化時間 */    public static String formatTime(String showTime) {        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Long                .valueOf(showTime));    }    /** 擷取時間的毫秒數 */    public static long getMillis() throws ParseException {        return System.currentTimeMillis();    }    /** 擷取指定時間的毫秒數,參數格式: yyyy-MM-dd HH:mm:ss,返回null代表計算失敗 */    public static long getMillis(String dstr) throws ParseException {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date date = sdf.parse(dstr);        long s1 = date.getTime();        return s1;    }    /** 返回 指定時間的毫秒數 減 當前日期的毫秒數;參數格式: yyyy-MM-dd HH:mm:ss,返回null代表計算失敗 */    public static long getMillisDistanceNow(String dstr) throws ParseException {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date date = sdf.parse(dstr);        long s1 = date.getTime();        long s2 = System.currentTimeMillis();        return s1 - s2;    }    /** 返回格式:2015-07-08 */    public static String getTodayDate() {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());        String now = sdf.format(Long.valueOf(System.currentTimeMillis()));        return now;    }    /** 返回格式:2015-07-08 08:00:00 */    public static String getTodayTime() {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());        String now = sdf.format(Long.valueOf(System.currentTimeMillis()));        return now;    }    /** 返回格式:2015-07-08 */    public static String getTomorrowDate() {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());        String date = sdf                .format(Long.valueOf(System.currentTimeMillis() + 1 * 24 * 60 * 60 * 1000));        return date;    }    /** 返回格式:2015-07-08 00:00:00 */    public static String getTomorrowTime() {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());        String now = sdf.format(Long.valueOf(System.currentTimeMillis() + 1 * 24 * 60 * 60 * 1000));        long zeroHourMillis = 0;        try {            zeroHourMillis = getMillis(now + " " + "00:00:00");        } catch (ParseException e) {            e.printStackTrace();        }        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());        String time = sdf.format(Long.valueOf(zeroHourMillis));        return time;    }    /** 比較日期的大小, */    public static int compareDate(String date1, String date2) {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());        Date d1 = null;        Date d2 = null;        try {            d1 = sdf.parse(date1);            d2 = sdf.parse(date2);        } catch (ParseException e) {            e.printStackTrace();            return -1;        }        return d1.compareTo(d2);    }    /** 比較日期與當前日期的大小, */    public static int compareDate(String dateStr) {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());        String now = sdf.format(Long.valueOf(System.currentTimeMillis()));        Date date = null;        Date nowdate = null;        try {            date = sdf.parse(dateStr);            nowdate = sdf.parse(now);        } catch (ParseException e) {            e.printStackTrace();            return -1;        }        return nowdate.compareTo(date);    }}


java時間類簡單整合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.