日期时间格式的工具DateUtils整理
关于一些时间工具的处理
* 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为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整理的更多相关文章
- 调用DEDE日期时间格式整理大全
dedecms 日期时间格式大全,大家可以根据需要选择.DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strf ...
- 转换GMT秒数为日期时间格式-Delphi源码
转换GMT秒数为日期时间格式-Delphi源码.收藏最近在写PE分析工具的时候,需要转换TimeDateStamp字段值为日期时间格式,这是Delphi的源码. //把GMT时间的秒数转换成日期时间格 ...
- python 日期、时间处理,各种日期时间格式/字符串之间的相互转换究竟是怎样的?
模块函数说明 ''' date 日期对象,常用的属性有year,month,day time 时间对象,常用的属性有hour,minute,second,毫秒 datetime 日期时间对象,常用的属 ...
- db2 日期时间格式
db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...
- SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- SQL Server日期时间格式转换字符串
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- 一起Polyfill系列:让Date识别ISO 8601日期时间格式
一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12 ...
- Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()
一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...
- WPF-数据绑定:日期时间格式
WPF-数据绑定:日期时间格式绑定后自定义格式的例子. 我刚才遇到的问题是绑定完之后,星期始终显示为英文.需要一个属性ConverterCulture制定区域. 如下: {Binding dateti ...
随机推荐
- aria2 adduri
demo, ok import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handl ...
- 【高并发解决方案】8、Nginx/LVS/HAProxy负载均衡软件的优缺点详解
PS:Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,参考了一些资料,结合自己的一些使用经验,总结一下. 一般对负载均衡的使用是随着网站规模的提升根据不 ...
- TypeScript基础以及在Vue中的应用
TypeScript推出已经很长时间了,在Angular项目中开发比较普遍,随着Vue 3.0的即将推出,TypeScript在Vue项目中使用也即将成为很大的趋势,笔者也是最近才开始研究如何在Vue ...
- 数据仓库模型ETL架构(DWI/DWR/DM)
1.DWI DWI:数据湖.数据砥柱,一般存放在HDFS 数据仓库的基础数据来源,各种杂七杂八的数据 关键点:数据清洗.数据整合.异常处理.增量获取 ETL:E-数据抽取.数据清洁.格式转换,T-生成 ...
- ora121 tips
1. 900929 - Linux: STORAGE_PARAMETERS_WRONG_SET and "mmap() failed" Solution Increase the ...
- 继 首次使用DoNetCore EFCore DbFirst 更新数据实体
//EFCore DB First 步骤 //第一步:Install-Package Microsoft.EntityFrameworkCore.SqlServer -version 2.1.1 // ...
- 根据不同配置.env获取不同的配置文件的配置
env 不同环境 1. 安装扩展 composer require vlucas/phpdotenv 2 . 创建文件 .env .env.test .env.production .env.exam ...
- 【TTS】传输表空间Linux asm -> AIX asm
[TTS]传输表空间Linux asm -> AIX asm 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌 ...
- JVM中的类加载
JVM中的类加载 关于JVM中类的加载这部分知识在网上有太多的文章描述这部分的知识.但是多数文章都过于冗长,难以理解.这篇文章主要是一些我对JVM中类的加载的理解. 一.一句话概括 java在类加载的 ...
- reboot 示例代码
#include <stdio.h> #define LINUX_REBOOT_CMD_RESTART 0x01234567 int main() { reboot(LINUX_REBOO ...