关于一些时间工具的处理

 * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串
* 1、得到当前日期 today()
* 2、得到当前月份月初 thisMonth()
* 3、得到当前月份月底 thisMonthEnd()
* 4、得到当前季度季初 thisSeason()
* 5、得到当前季度季末 thisSeasonEnd()
* 6、得到当前年份年初 thisYear()
* 7、得到当前年份年底 thisYearEnd()
* 8、判断输入年份是否为闰年 leapYear
* 9、得到当前的月份 localMonth()
* 注意事项: 日期格式为:yyyy-MM-dd (eg: 2007-12-05)
import java.util.Calendar;

/**
* 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串
* 1、得到当前日期 today()
* 2、得到当前月份月初 thisMonth()
* 3、得到当前月份月底 thisMonthEnd()
* 4、得到当前季度季初 thisSeason()
* 5、得到当前季度季末 thisSeasonEnd()
* 6、得到当前年份年初 thisYear()
* 7、得到当前年份年底 thisYearEnd()
* 8、判断输入年份是否为闰年 leapYear
* 9、得到当前的月份 localMonth()
* <p>
* 注意事项: 日期格式为:yyyy-MM-dd (eg: 2007-12-05)
* <p>
* 实例:
*
* @author pure
*/
public class DateUtils {
private int x; // 日期属性:年
private int y; // 日期属性:月
private int z; // 日期属性:日
private Calendar localTime; // 当前日期 public DateUtils() {
localTime = Calendar.getInstance();
} /**
* 功能:得到当前日期 格式为:yyyy-MM-dd (eg: 2007-12-05)<br>
*
* @return String
* @author pure
*/
public String today() {
String strY = null;
String strZ = null;
x = localTime.get(Calendar.YEAR);
y = localTime.get(Calendar.MONTH) + 1;
z = localTime.get(Calendar.DATE);
strY = y >= 10 ? String.valueOf(y) : ("0" + y);
strZ = z >= 10 ? String.valueOf(z) : ("0" + z);
return x + "-" + strY + "-" + strZ;
}
/**
* 功能:得到当前月份 格式为:yyyy.MM (eg: 2007.12)<br>
*
* @return String
* @author pure
*/
public String thisMonth() {
String strY = null;
x = localTime.get(Calendar.YEAR);
y = localTime.get(Calendar.MONTH) + 1;
strY = y >= 10 ? String.valueOf(y) : ("0" + y);
return x + "." + strY ;
}
/**
* 功能:得到当前月份月初 格式为:yyyy-MM-dd (eg: 2007-12-01)<br>
*
* @return String
* @author pure
*/
public String thisMonthStart() {
String strY = null;
x = localTime.get(Calendar.YEAR);
y = localTime.get(Calendar.MONTH) + 1;
strY = y >= 10 ? String.valueOf(y) : ("0" + y);
return x + "-" + strY + "-01";
} /**
* 功能:得到当前月份月底 格式为:yyyy-MM-dd (eg: 2007-12-31)<br>
*
* @return String
* @author pure
*/
public String thisMonthEnd() {
String strY = null;
String strZ = null;
boolean leap = false;
x = localTime.get(Calendar.YEAR);
y = localTime.get(Calendar.MONTH) + 1;
if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
strZ = "31";
}
if (y == 4 || y == 6 || y == 9 || y == 11) {
strZ = "30";
}
if (y == 2) {
leap = leapYear(x);
if (leap) {
strZ = "29";
} else {
strZ = "28";
}
}
strY = y >= 10 ? String.valueOf(y) : ("0" + y);
return x + "-" + strY + "-" + strZ;
} /**
* 功能:得到当前季度季初 格式为:yyyy-MM-dd (eg: 2007-10-01)<br>
*
* @return String
* @author pure
*/
public String thisSeason() {
String dateString = "";
x = localTime.get(Calendar.YEAR);
y = localTime.get(Calendar.MONTH) + 1;
if (y >= 1 && y <= 3) {
dateString = x + "-" + "01" + "-" + "01";
}
if (y >= 4 && y <= 6) {
dateString = x + "-" + "04" + "-" + "01";
}
if (y >= 7 && y <= 9) {
dateString = x + "-" + "07" + "-" + "01";
}
if (y >= 10 && y <= 12) {
dateString = x + "-" + "10" + "-" + "01";
}
return dateString;
} /**
* 功能:得到当前季度季末 格式为:yyyy-MM-dd (eg: 2007-12-31)<br>
*
* @return String
* @author pure
*/
public String thisSeasonEnd() {
String dateString = "";
x = localTime.get(Calendar.YEAR);
y = localTime.get(Calendar.MONTH) + 1;
if (y >= 1 && y <= 3) {
dateString = x + "-" + "03" + "-" + "31";
}
if (y >= 4 && y <= 6) {
dateString = x + "-" + "06" + "-" + "30";
}
if (y >= 7 && y <= 9) {
dateString = x + "-" + "09" + "-" + "30";
}
if (y >= 10 && y <= 12) {
dateString = x + "-" + "12" + "-" + "31";
}
return dateString;
} /**
* 功能:得到当前年份年初 格式为:yyyy-MM-dd (eg: 2007-01-01)<br>
*
* @return String
* @author pure
*/
public String thisYear() {
x = localTime.get(Calendar.YEAR);
return x + "-01" + "-01";
} /**
* 功能:得到当前年份年底 格式为:yyyy-MM-dd (eg: 2007-12-31)<br>
*
* @return String
* @author pure
*/
public String thisYearEnd() {
x = localTime.get(Calendar.YEAR);
return x + "-12" + "-31";
} /**
* 功能:判断输入年份是否为闰年<br>
*
* @param year
* @return 是:true 否:false
* @author pure
*/
public boolean leapYear(int year) {
boolean leap;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) leap = true;
else leap = false;
} else leap = true;
} else leap = false;
return leap;
} /**
* 功能:返回月份
*
* @param i 返回近几月就写几
* @return 例:当前2月,i=0,返回2月;i=1,返回1月;i=2,返回上12月
*/
public String localMonth(int i) {
localTime.add(Calendar.MONTH, -i);
y = localTime.get(Calendar.MONTH) + 1;
return Integer.toString(y);
} /**
* 功能:几个月的时间范围,格式为:yyyy-MM-dd (eg: 2007-12-31)<br>
*
* @return 例:当前2018-3-10,1个月范围,返回2018-2-28
*/
public String lastMonthDay(int month) { String strY = null;
String strZ = null;
boolean leap = false;
localTime.add(Calendar.MONTH, -month);
x = localTime.get(Calendar.YEAR);
y = localTime.get(Calendar.MONTH) + 1;
if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
strZ = "31";
}
if (y == 4 || y == 6 || y == 9 || y == 11) {
strZ = "30";
}
if (y == 2) {
leap = leapYear(x);
if (leap) {
strZ = "29";
} else {
strZ = "28";
}
}
strY = y >= 10 ? String.valueOf(y) : ("0" + y);
return x + "-" + strY + "-" + strZ;
} }

日期时间格式的工具DateUtils整理的更多相关文章

  1. 调用DEDE日期时间格式整理大全

    dedecms 日期时间格式大全,大家可以根据需要选择.DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strf ...

  2. 转换GMT秒数为日期时间格式-Delphi源码

    转换GMT秒数为日期时间格式-Delphi源码.收藏最近在写PE分析工具的时候,需要转换TimeDateStamp字段值为日期时间格式,这是Delphi的源码. //把GMT时间的秒数转换成日期时间格 ...

  3. python 日期、时间处理,各种日期时间格式/字符串之间的相互转换究竟是怎样的?

    模块函数说明 ''' date 日期对象,常用的属性有year,month,day time 时间对象,常用的属性有hour,minute,second,毫秒 datetime 日期时间对象,常用的属 ...

  4. db2 日期时间格式

    db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...

  5. SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  6. SQL Server日期时间格式转换字符串

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  7. 一起Polyfill系列:让Date识别ISO 8601日期时间格式

    一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12 ...

  8. Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()

    一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...

  9. WPF-数据绑定:日期时间格式

    WPF-数据绑定:日期时间格式绑定后自定义格式的例子. 我刚才遇到的问题是绑定完之后,星期始终显示为英文.需要一个属性ConverterCulture制定区域. 如下: {Binding dateti ...

随机推荐

  1. exchange From Middle English eschaunge

    exchange From Middle English eschaunge, borrowed from Anglo-Norman eschaunge exchange 1.An act of ex ...

  2. ABAP-System Functions

    ABAP_CALLSTACK ABAP_CRC64 ABAP_PRECOMPILED_HEADER_USAGE ABSTOR_TEST AB_CALL_LITL_CHECK AB_CALL_STACK ...

  3. centos8 网络配置

    目录 centos8已经发布了,下载了一个体验一下,新安装好的centos8默认网卡是没有启动的,安装好后需要先配置网络.在/etc/sysconfig/network-scripts目录下存放着网卡 ...

  4. shell输出文本颜色

    绿地白字 echo -e "\033[42;37m 绿底白字 \033[0m"

  5. TensorFlow 2 快速教程,初学者入门必备

    TensorFlow 2 简介 TensorFlow 是由谷歌在 2015 年 11 月发布的深度学习开源工具,我们可以用它来快速构建深度神经网络,并训练深度学习模型.运用 TensorFlow 及其 ...

  6. Linux命令——chattr、lsattr

    简介 chattr用于设置文件隐藏属性,lsattr用于查看文件隐藏属性.隐藏属性对系统很有用,尤其是系统安全这一块.但是这两个命令只能在Ext2/Ext3上面有用,其他文件系统可能不支持. chat ...

  7. 关于python模块总结

    名词解释 模块(module):在python中,一个.py文件就是一个模块 包(package):为了避免模块名冲突,Python又引入了按目录来组织模块的方法.当目录下存在__init__.py, ...

  8. 2019年牛客多校第一场 H题XOR 线性基

    题目链接 传送门 题意 求\(n\)个数中子集内所有数异或为\(0\)的子集大小之和. 思路 对于子集大小我们不好维护,因此我们可以转换思路变成求每个数的贡献. 首先我们将所有数的线性基的基底\(b\ ...

  9. Vue的参数请求与传递

    Vue不同模板之间的参数传递 页面路由带参数的跳转: 参数接收: Vue向服务器请求资源的两种方式 VUE-RESOURCE 1.Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不 ...

  10. 深度学习Keras框架笔记之TimeDistributedDense类

    深度学习Keras框架笔记之TimeDistributedDense类使用方法笔记 例: keras.layers.core.TimeDistributedDense(output_dim,init= ...