• 时间日期类


    package com.mw.platform.util;

    import java.io.Serializable;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;

    //时间类(参考了.net 的接口)
    //
    // demo:: int date = Datetime.Now().addDay(-3).getDate();
    //
    public class Datetime implements Serializable {
    private Date _datetime;
    private Calendar _calendar = null;

    public Datetime(){
    setFulltime(new Date());
    }

    public Datetime(Date date) {
    setFulltime(date);
    }

    public Datetime(long milliseconds) {
    setFulltime(new Date(milliseconds));
    }

    public Datetime setFulltime(Date date) {
    _datetime = date;
    _calendar = Calendar.getInstance();
    _calendar.setTime(date);
    return this;
    }

    /* public Datetime setFulltime1(Date date){
    _datetime = date;
    _calendar = Calendar.getInstance();
    _calendar.setTime(date);
    return this;
    }*/

    public Date getFullTime() {
    return _datetime;
    }

    // 当前时间
    public static Datetime Now() {
    return new Datetime(new Date());
    }

    // 添加年
    public Datetime addYear(int year) {
    return doAdd(Calendar.YEAR, +year);
    }

    // 添加月
    public Datetime addMonth(int month) {
    return doAdd(Calendar.MONTH, +month);
    }

    // 添加日
    public Datetime addDay(int day) {
    return doAdd(Calendar.DAY_OF_MONTH, +day);
    }

    // 添加小时
    public Datetime addHour(int hour) {
    return doAdd(Calendar.HOUR_OF_DAY, +hour);
    }

    // 添加分钟
    public Datetime addMinute(int minute) {
    return doAdd(Calendar.MINUTE, +minute);
    }

    // 添加秒
    public Datetime addSecond(int minute) {
    return doAdd(Calendar.SECOND, +minute);
    }

    private Datetime doAdd(int field, int amount) {
    _calendar.add(field, +amount);

    _datetime = _calendar.getTime();

    return this;
    }

    // 获取当前年份
    public int getYear() {
    return _calendar.get(Calendar.YEAR);
    }

    // 获取当前月份
    public int getMonth() {
    return _calendar.get(Calendar.MONTH);
    }

    // 获取当前日份
    public int getDays() {
    return _calendar.get(Calendar.DAY_OF_MONTH);
    }

    // 获取当前小时
    public int getHours() {
    return _calendar.get(Calendar.HOUR_OF_DAY);
    }

    // 获取当前分钟
    public int getMinutes() {
    return _calendar.get(Calendar.MINUTE);
    }

    // 获取当前秒数
    public int getSeconds() {
    return _calendar.get(Calendar.SECOND);
    }

    // 获取当前豪秒
    public long getMilliseconds() {
    return _calendar.get(Calendar.MILLISECOND);
    }

    // 获取总天数(相对于:1970.01.01 00:00:00 GMT)
    public long getAllDays() {
    return getAllHours() / 24;
    }

    // 获取总小时(相对于:1970.01.01 00:00:00 GMT)
    public long getAllHours() {
    return getAllMinutes() / 60;
    }

    // 获取总分钟(相对于:1970.01.01 00:00:00 GMT)
    public long getAllMinutes() {
    return getAllSeconds() / 60;
    }

    // 获取总秒(相对于:1970.01.01 00:00:00 GMT)
    public long getAllSeconds() {
    return getTicks() / 1000;
    }

    // 获取总毫秒(相对于:1970.01.01 00:00:00 GMT)
    public long getAllMilliseconds() {
    return getTicks();
    }

    // 获取计时周期数(相对于:1970.01.01 00:00:00 GMT)
    public long getTicks() {
    return _datetime.getTime();
    }

    // 获取日期数字(yyyyMMdd)
    public int getDate() {
    return Integer.parseInt(toString("yyyyMMdd"));
    }

    // 转成String
    public String toString(String format) {
    DateFormat df = new SimpleDateFormat(format);
    return df.format(_datetime);
    }
    /*public String toString1(String format){
    DateFormat df1= new SimpleDateFormat(format);
    return df1.format(-_datetime);
    }*/
    @Override
    public String toString() {
    return toString("yyyy-MM-dd HH:mm:ss");
    }

    /**
    * @return 获取指定日期的本月第一天
    */
    public Datetime getMonthFirstDay() {
    _calendar.add(Calendar.MONTH, 0);
    _calendar.set(Calendar.DAY_OF_MONTH, 1);
    Datetime datetime = new Datetime(_calendar.getTime());
    return datetime;
    }

    /*public Datetime getMonthFirstDay1(){
    _calendar.add(Calendar.MONTH,0);
    _calendar.set(Calendar.DAY_OF_MONTH,1);
    Datetime datetime = new Datetime(_calendar.getTime());
    return datetime;
    }*/

    /**
    * @return 获取指定日期的本月最后一天
    */
    public Datetime getMonthLastDay() {
    _calendar.set(Calendar.DAY_OF_MONTH,
    _calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    Datetime datetime = new Datetime(_calendar.getTime());
    return datetime;
    }



    /**
    * @return 获取指定日期的周一日期
    */
    public Datetime getTimesWeekmorning() {
    _calendar.set(_calendar.get(Calendar.YEAR), _calendar.get(Calendar.MONDAY),
    _calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
    _calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    Datetime datetime = new Datetime(_calendar.getTime());
    return datetime;
    }

    /**
    * @return 获取指定日期的周日日期
    */
    public Datetime getTimesWeeknight() {
    _calendar.setTime(getTimesWeekmorning().getFullTime());
    _calendar.add(Calendar.DAY_OF_WEEK, 6);
    Datetime datetime = new Datetime(_calendar.getTime());
    return datetime;
    }

    // ===================
    //
    public static Datetime parse(String datetime, String format)
    throws ParseException {
    DateFormat df = new SimpleDateFormat(format);
    Date date = df.parse(datetime);
    return new Datetime(date);
    }

    public static Datetime tryParse(String datetime, String format) {
    DateFormat df = new SimpleDateFormat(format);

    try {
    Date date = df.parse(datetime);
    return new Datetime(date);
    } catch (Exception ex) {
    return null;
    }
    }

    }
  • 相关阅读:
    AgularJS中Unknown provider: $routeProvider解决方案
    AngularJS依赖注入
    下面是Webstorm的一些常用快捷键:
    加载多张图片,判断加载完成状态
    JS控制图片显示的大小(图片等比例缩放)
    JS中两个重要的方法 call & apply 学习
    如何用微软雅黑显示自己网页的字体
    Dev-C++的一些使用技巧快捷键
    C C语言中 *.c和*.h文件的区别!
    HTML <!--...--> 注释 、CSS/JS //注释 和 /*.....*/ 注释
  • 原文地址:https://www.cnblogs.com/21-forever/p/11389333.html
Copyright © 2020-2023  润新知