Java 仿c# DateTime辅助类

           目的:辅助使用c#习惯的小伙伴使用DateTime来处理时间问题;

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

public class DateTime
{
    static private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Date date;
    Calendar calendar;

    public DateTime()
    {
        date = new Date();
        calendar = Calendar.getInstance();
        calendar.setTime(date);
    }

    public DateTime(String time)
    {
        try {
            date = new Date();
            date = dateFormat.parse(time);
            calendar = Calendar.getInstance();
            calendar.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public DateTime(long allMillisec)
    {
        date = new Date(allMillisec);
        calendar.setTime(date);
    }

    public DateTime(int year, int month, int day)
    {
        date = new Date(year, month, day);
        calendar = Calendar.getInstance();
        calendar.set(year, month, day);
    }

    public DateTime(int year, int month, int day, int hour, int minute, int second)
    {
        date = new Date(year, month, day, hour, minute, second);
        calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, minute, second);
    }

    public long getAllMillisec()
    {
        return date.getTime();
    }

    public static DateTime Now()
    {
        return new DateTime();
    }

    public int getYear()
    {
        return calendar.get(Calendar.YEAR);
    }

    public int getMonth()
    {
        return calendar.get(Calendar.MONTH);
    }

    public int getDay()
    {
        return calendar.get(Calendar.DATE);
    }

    public int getHour()
    {
        return calendar.get(Calendar.HOUR);
    }

    public int getMinute()
    {
        return calendar.get(Calendar.MINUTE);
    }

    public int getSecond()
    {
        return calendar.get(Calendar.SECOND);
    }

    public String getWeek()
    {
        String[] week={"", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        return week[Calendar.DAY_OF_WEEK];
    }

    public Calendar getCalendar()
    {
        return calendar;
    }
    public void setDate(Date dt)
    {
        this.date = dt;
        this.calendar.setTime(dt);
    }

    public DateTime AddSeconds(int seconds)
    {
        DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
        time.getCalendar().add(Calendar.SECOND, seconds);
        time.setDate(time.getCalendar().getTime());
        return time;
    }

    public DateTime AddMinutes(int minutes)
    {
        DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
        time.getCalendar().add(Calendar.MINUTE, minutes);
        time.setDate(time.getCalendar().getTime());
        return time;
    }

    public DateTime AddDays(int days)
    {
        DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
        time.getCalendar().add(Calendar.DATE, days);
        time.setDate(time.getCalendar().getTime());
        return time;
    }

    public DateTime AddMonths(int months)
    {
        DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
        time.getCalendar().add(Calendar.MONTH, months);
        time.setDate(time.getCalendar().getTime());
        return time;
    }

    public DateTime AddYears(int years)
    {
        DateTime time = new DateTime(this.toString("yyyy-MM-dd HH:mm:ss"));
        time.getCalendar().add(Calendar.YEAR, years);
        time.setDate(time.getCalendar().getTime());
        return time;
    }

    public String toString(String format)
    {
        DateFormat dtDateFormat = new SimpleDateFormat(format);
        return dtDateFormat.format(date);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_bill

老板大气!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值