package cn.com.worksoft.util.datetime;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.com.worksoft.util.basetype.CastType;

/**
* @author XING 日期时间工具类。
*/
public final class TDateTime implements java.io.Serializable, Comparable {
private final static int[] full_day_month = new int[] { 1, 3, 5, 7, 8, 10,
12 };

/**
* The year of the gregorianCutover, with 0 representing 1 BC, -1
* representing 2 BC, etc.
*/
private static transient int gregorianCutoverYear = 1582;

private final static int[] normal_day_month = new int[] { 4, 6, 9, 11};

public final static int QUARTER_OF_FIRST = 1;

public final static int QUARTER_OF_FOURTH = 4;

public final static int QUARTER_OF_SECOND = 2;

public final static int QUARTER_OF_THIRD = 3;

/**
*
*/
private static final long serialVersionUID = 1574586998788149344L;

/**
* 当前日期时间。
*
* @return
*/
public static TDateTime CurrentDateTime() {
return new TDateTime();
}

/**
* 取得某年月最大日。
*
* @param y
* @param m
* @return
*/
public static int getMaxDay(int y, int m) {
if (isLeapNormalMonth(y, m))
return 29;
else if (isNormalMonth(y, m))
return 28;
else if (isSmallMonth(m))
return 30;
else
return 31;
}

/**
* 是否为大月
*
* @param m
* @return
*/
public static boolean isBigMonth(int m) {
for (int i = 0; i < full_day_month.length; i++) {
if (m == full_day_month[i])
return true;
}
return false;
}

/**
* 是否为闰平月
*
* @param year
* @param month
* @return
*/
public static boolean isLeapNormalMonth(int y, int m) {
return isleapyear(y) && m == 2;
}

/**
* 是否闰年。
*
* @param y
* @return
*/
public static boolean isleapyear(int y) {
return y >= gregorianCutoverYear ? ((y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0)))
: // Gregorian
(y % 4 == 0); // Julian
}

/**
* 是否为普通平月
*
* @param year
* @param month
* @return
*/
public static boolean isNormalMonth(int y, int m) {
return !isleapyear(y) && m == 2;
}

/**
* 是否为小月
*
* @param m
* @return
*/
public static boolean isSmallMonth(int m) {
for (int i = 0; i < normal_day_month.length; i++) {
if (m == normal_day_month[i])
return true;
}
return false;
}

public static void main(String[] args) throws Exception {
// System.out.println(CastType.dtparse("2005-3-3 00:00:00"));
TDateTime tdt1 = new TDateTime("2005-3-32 10:00:00");
TDateTime tdt2 = new TDateTime("2005-4-1 11:00:00");
// TDateTime tdt1 = new TDateTime(2000, 2, 3, 4, 5, 6, 7);
System.out.println(tdt1);
System.out.println(tdt2);
System.out.println(tdt1.getDayDistance(tdt2));
System.out.println(tdt1);
System.out.println(tdt2);
}

private Calendar cal = null;

private Date dt = null;

private Timestamp ts = null;

public TDateTime() {
cal = Calendar.getInstance();
dt = cal.getTime();
ts = new Timestamp(cal.getTimeInMillis());
}

public TDateTime(Calendar cal) {
init(cal);
}

public TDateTime(Date dt) {
init(dt);
}

public TDateTime(int year) {
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
init(cal);
}

public TDateTime(int year, int month) {
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
init(cal);
}

public TDateTime(int year, int month, int day) {
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
init(cal);
}

public TDateTime(int year, int month, int day, int hour) {
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
init(cal);
}

public TDateTime(int year, int month, int day, int hour, int minute) {
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
init(cal);
}

public TDateTime(int year, int month, int day, int hour, int minute,
int second) {
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
init(cal);
}

public TDateTime(int year, int month, int day, int hour, int minute,
int second, int millisecond) {
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
cal.set(Calendar.MILLISECOND, millisecond);
init(cal);
}

public TDateTime(String value) throws Exception {
this(CastType.dtparse(value));
}

public TDateTime(TDateTime tdt) {
cal = (Calendar) tdt.cal.clone();
init(cal);
}

public TDateTime(Timestamp ts) {
init(ts);
}

/**
* 增加日期。
*
* @param add
*/
public void addDay(int add) {
cal.add(Calendar.DATE, add);
init(cal);
}

/**
* 增加小时。
*
* @param add
*/
public void addHour(int add) {
cal.add(Calendar.HOUR_OF_DAY, add);
init(cal);
}

/**
* 增加毫秒。
*
* @param add
*/
public void addMillisecond(int add) {
cal.add(Calendar.MILLISECOND, add);
init(cal);
}

/**
* 增加分钟。
*
* @param add
*/
public void addMinute(int add) {
cal.add(Calendar.MINUTE, add);
init(cal);
}

/**
* 增加月。
*
* @param add
*/
public void addMonth(int add) {
cal.add(Calendar.MONTH, add);
init(cal);
}

/**
* 增加秒。
*
* @param add
*/
public void addSecond(int add) {
cal.add(Calendar.SECOND, add);
init(cal);
}

/**
* 增加年份。
*
* @param add
*/
public void addYear(int add) {
cal.add(Calendar.YEAR, add);
init(cal);
}

/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Object o) {
if (o == null) {
return 1;
}
if (o instanceof TDateTime) {
return compareTo((TDateTime) o);
}
throw new UnknowRuntimeException("can't compare TDateTime with "
+ o.getClass());
}

public int compareTo(TDateTime tdt) {
if (tdt == null) {
return 1;
}
return dt.compareTo(tdt.dt);
}

/**
* 取得Calendar数据。
*
* @return
*/
public Calendar getCalendar() {
return cal;
}

/**
* 取得Date数据。
*
* @return
*/
public Date getDate() {
return dt;
}

/**
* 取得日期。
*
* @return Returns the day.
*/
public int getDay() {
return cal.get(Calendar.DATE);
}

/**
* 取得两个日期相隔天数。 <br>
* 如果比参数日大,返回正数,否则如果小,返回负数,否则返回零。
*
* @param tdt
* @return
*/
public int getDayDistance(TDateTime tdt) {
tdt = new TDateTime(tdt);
TDateTime clone = new TDateTime(this);
clone.setHour(0);
clone.setMinute(0);
clone.setSecond(0);
clone.setMillisecond(0);
tdt.setHour(0);
tdt.setMinute(0);
tdt.setSecond(0);
tdt.setMillisecond(0);

int day = 0;
if (clone.compareTo(tdt) > 0) {
while (clone.compareTo(tdt) > 0) {
day++;
clone.addDay(-1);
}
} else {
while (clone.compareTo(tdt) < 0) {
day--;
clone.addDay(1);
}
}
return day;
}

/**
* 取得小时。
*
* @return Returns the hour.
*/
public int getHour() {
return cal.get(Calendar.HOUR_OF_DAY);
}

/**
* 取得本月最大天数。
*
* @return
*/
public int getMaxDay() {
return getMaxDay(getYear(), getMonth());
}

/**
* 取得毫秒。
*
* @return Returns the millisecond.
*/
public int getMillisecond() {
return cal.get(Calendar.MILLISECOND);
}

/**
* 取得分钟。
*
* @return Returns the minute.
*/
public int getMinute() {
return cal.get(Calendar.MINUTE);
}

/**
* 取得月份。
*
* @return Returns the month.
*/
public int getMonth() {
return cal.get(Calendar.MONTH) + 1;
}

/**
* 取得季节。
*
* @return
*/
public int getQuarter() {
int month = getMonth();
switch (month) {
case 1:
case 2:
case 3:
return TDateTime.QUARTER_OF_FIRST;
case 4:
case 5:
case 6:
return TDateTime.QUARTER_OF_SECOND;
case 7:
case 8:
case 9:
return TDateTime.QUARTER_OF_THIRD;
case 10:
case 11:
case 12:
return TDateTime.QUARTER_OF_FOURTH;
}
throw new UnknowRuntimeException("month is error");
}

/**
* 取得秒。
*
* @return Returns the second.
*/
public int getSecond() {
return cal.get(Calendar.SECOND);
}

/**
* 获得Timestamp类型数据。
*
* @return
*/
public Timestamp getTimestamp() {
return ts;
}

/**
* 取得年。
*
* @return Returns the year.
*/
public int getYear() {
return cal.get(Calendar.YEAR);
}

/**
* @param c
*/
private void init(Calendar c) {
cal = c;
dt = cal.getTime();
ts = new Timestamp(cal.getTimeInMillis());
}

/**
* @param d
*/
private void init(Date d) {
dt = d;
ts = new Timestamp(d.getTime());
cal = Calendar.getInstance();
cal.setTimeInMillis(ts.getTime());
}

/**
* @param t
*/
private void init(Timestamp t) {
ts = t;
cal = Calendar.getInstance();
cal.setTimeInMillis(ts.getTime());
dt = cal.getTime();
}

/**
* 设置日期。
*
* @param day
*            The day to set.
*/
public void setDay(int day) {
cal.set(Calendar.DATE, day);
init(cal);
}

/**
* 设置小时。
*
* @param hour
*            The hour to set.
*/
public void setHour(int hour) {
cal.set(Calendar.HOUR_OF_DAY, hour);
init(cal);
}

/**
* 设置毫秒。
*
* @param millisecond
*            The millisecond to set.
*/
public void setMillisecond(int millisecond) {
cal.set(Calendar.MILLISECOND, millisecond);
init(cal);
}

/**
* 设置分钟。
*
* @param minute
*            The minute to set.
*/
public void setMinute(int minute) {
cal.set(Calendar.MINUTE, minute);
init(cal);
}

/**
* 设置月份。
*
* @param month
*            The month to set.
*/
public void setMonth(int month) {
cal.set(Calendar.MONTH, month - 1);
init(cal);
}

/**
* 设置秒。
*
* @param second
*            The second to set.
*/
public void setSecond(int second) {
cal.set(Calendar.SECOND, second);
init(cal);
}

/**
* 设置年。
*
* @param year
*            The year to set.
*/
public void setYear(int year) {
cal.set(Calendar.YEAR, year);
init(cal);
}

/**
* 返回日期串。
*
* @return
*/
public String toDateString() {
return toString("yyyy-MM-dd");
}

/**
* 返回日期时间串
*
* @return
*/
public String toDateTimeString() {
return toString("yyyy-MM-dd HH:mm:ss.SSS");
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("TDateTime");
sb.append("[");
sb.append(getTimestamp());
sb.append("]");
sb.append("[");
sb.append("Year:");
sb.append(getYear());
sb.append(",");
sb.append("Month:");
sb.append(getMonth());
sb.append(",");
sb.append("Day:");
sb.append(getDay());
sb.append(",");
sb.append("Hour:");
sb.append(getHour());
sb.append(",");
sb.append("Minute:");
sb.append(getMinute());
sb.append(",");
sb.append("Second:");
sb.append(getSecond());
sb.append(",");
sb.append("MilliSecond:");
sb.append(getMillisecond());
sb.append("]");
return sb.toString();
}

/**
* <pre>
*                              时间格式语法:
*                        
*                              使用一个 time pattern 字符串指定时间格式。 在这种方式下,所有的 ASCII 字母被保留为模式字母,定义如下:
*                        
*                              符号     含义                    表示                示例
*                              ------   -------                 ------------        -------
*                              G        年代标志符              (Text)              AD
*                              y        年                      (Number)            1996
*                              M        月                      (Text & Number)     July & 07
*                              d        日                      (Number)            10
*                              h        时 在上午或下午 (1˜12)  (Number)            12
*                              H        时 在一天中 (0˜23)      (Number)            0
*                              m        分                      (Number)            30
*                              s        秒                      (Number)            55
*                              S        毫秒                    (Number)            978
*                              E        星期                    (Text)              Tuesday
*                              D        一年中的第几天          (Number)            189
*                              F        一月中第几个星期几      (Number)            2  (2nd Wed in July)
*                              w        一年中第几个星期        (Number)            27
*                              W        一月中第几个星期        (Number)            2
*                              a        上午 / 下午 标记符      (Text)              PM
*                              k        时 在一天中 (1˜24)      (Number)            24
*                              K        时 在上午或下午 (0˜11)  (Number)            0
*                              z        时区                    (Text)      Pacific Standard Time
*                              '        文本转义符              (Delimiter)
*                              ''       单引号                  (Literal)           '
* </pre>
*
* @param parttern
* @return
*/
/**
* @param parttern
* @return
*/
public String toString(String parttern) {
SimpleDateFormat sdf = new SimpleDateFormat(parttern);
return sdf.format(dt);
}

/**
* 返回短时间类型。
*
* @return
*/
public String toShortTimeString() {
return toString("HH:mm");
}

/**
* 返回时间串。
*
* @return
*/
public String toTimeString() {
return toString("HH:mm:ss.SSS");
}
}

java 日期 运算相关推荐

  1. Java日期时间使用总结

    一.Java中的日期概述 日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式都是非常复杂的问题. 在Java中 ...

  2. 详解 Java 日期与时间

    文章目录 一.时区 二.夏令时 三.旧 API 3.1. Date 3.2. SimpleDateFormat 3.3. Calendar 四.新 API 4.1. LocalDateTime 4.2 ...

  3. java日期和时间操作

    Java日期处理 参考 https://juejin.cn/post/6844903560375697422 https://mp.weixin.qq.com/s/q__JSyLt1iy4h3NwZ6 ...

  4. Java日期时间类及计算

    1. Java中与日期相关的类 1.1 java.util包 类名 具体描述 Date Date对象算是JAVA中历史比较悠久的用于处理日期.时间相关的类了,但是随着版本的迭代演进,其中的众多方法都已 ...

  5. Java日期时间调整的几种方式

    一.Calendar类 我们现在已经能够格式化并创建一个日期对象了,但是我们如何才能设置和获取日期数据的特定部分呢,比如说小时,日,或者分钟? 我们又如何在日期的这些部分加上或者减去值呢? 答案是使用 ...

  6. Java日期时间(LocalDate、LocalTime、LocalDateTime)

    文章目录 Java日期时间(LocalDate.LocalTime.LocalDateTime) 前言 1.LocalDate 1.1.LocalDate常用API 1.2.LocalDate 与 S ...

  7. Java 日期和时间

    Java 日期和时间 日期和时间在我们的实际开发中非常常用,例如用户的注册.数据的增删改.对敏感信息的操作等等都需要记录下日期和时间.通过本小节的学习,你将了解到什么是日期.什么是时间.什么是时区,J ...

  8. java 日期和字符串互转,根据当天整天时间 得到当天最后一秒的日期时间

    2019独角兽企业重金招聘Python工程师标准>>> java 日期和字符串互转,根据当天整天时间   得到当天最后一秒的日期时间 package com.hi;import ja ...

  9. Java二进制位运算、移位运算、、

    为什么80%的码农都做不了架构师?>>>    Java二进制位运算.移位运算 思考题 1.请看下面的代码段,回答a,b,c,d,e结果是多少? public static void ...

最新文章

  1. mysql-cluster集群原理介绍和搭建步骤(四个data/sql节点) (转)
  2. PL/SQL Developer 导出表结构和表数据
  3. IBase component cannot be created and deleted in the same second
  4. mysql数据类型的学习心得_MySQL自学总结 (一到四章)
  5. 牛客网Java刷题知识点之表达式类型的自动提升
  6. oracle数据库中sql语句性能提升之to_char改造
  7. Visual FoxPro权威指南写作原稿及光盘下载
  8. C# 多文件压缩与解压
  9. ADX趋势线突破交易系统
  10. 服务器进pe后找不到硬盘,进入PE后找不到硬盘的原因及解决方法
  11. 你若运行,便是晴天!
  12. 华硕电脑连接不上wifi_华硕电脑连不上无线网怎么办
  13. 为什么角膜塑形镜的价格比隐形眼镜贵这么多?
  14. Java学习笔记(三):流程控制
  15. 《淘宝网开店 拍摄 修图 设计 装修 实战150招》一一2.9 疏密相间构图法
  16. java实现DSA签名、验签
  17. OPENWRT MT7628 驱动移植WIFI折腾记
  18. 计算机网络信宿是什么,计算机网络_第2章习题答案
  19. 计算机教育学校排名,教育知识:2020全国最好的计算机专业学校排名
  20. 使用Qt创建一个C语言工程

热门文章

  1. 对抗攻击公开课第二弹来啦,真题演练 + 代码实战
  2. 实用教程:如何制作学术会议Oral/Spotlight Video?
  3. CVPR2019接收结果公布了,但CVPR 2018的那些论文都怎么样了?
  4. NeurIPS 2018 | 腾讯AI Lab:可自适应于不同环境和任务的强化学习方法
  5. 5 篇 AAAI 2018 论文看「应答生成」
  6. 综述 | 知识图谱发展概述
  7. 两种方法判断有向图是否有环【DFS】【拓扑排序】
  8. spring原始注解(value)-03
  9. Tomcat线程连接池参数优化
  10. Spring Security——org.springframework.security.oauth:spring-security-oauth2项目已过时解决方案