关于时间的util类,以后方便查阅
public static int lastDayOfMonth(int year, int month)
{
if (month == 2)
{
if (isLeapYear(year)) {
return 29;
}
return 28;
}
if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) ||
(month == 12)) {
return 31;
}
return 30;
}
public static boolean isLeapYear(int year)
{
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
return true;
}
return false;
}
public static Date dateInstance(int year, int month, int day)
{
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day, 0, 0, 0);
return c.getTime();
}
public static Date dateInstance(int year, int month, int day, int hour, int min, int sec)
{
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day, hour, min, sec);
return c.getTime();
}
public static Calendar calendarInstance(Date d)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
return c;
}
public static int weekDay(Date d)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
return c.get(7);
}
public static int weekCountOfMonth(int year, int month)
{
int lastDay = lastDayOfMonth(year, month);
Calendar c = Calendar.getInstance();
c.set(year, month - 1, lastDay);
return c.get(4);
}
public static int firstDayOfWeekDay(int year, int month, int argWeekDay)
{
Date d = dateInstance(year, month, 1);
int weekday = weekDay(d);
if (weekday == argWeekDay) {
return 1;
}
if (weekday > argWeekDay) {
return 1 + (7 - (weekday - argWeekDay));
}
return 1 + (argWeekDay - weekday);
}
public static int lastDayOfWeekDay(int year, int month, int argWeekDay)
{
int lastDay = lastDayOfMonth(year, month);
int weekday = weekDay(dateInstance(year, month, lastDay));
if (weekday == argWeekDay) {
return lastDay;
}
if (weekday < argWeekDay) {
return lastDay - (7 - (argWeekDay - weekday));
}
return lastDay - (weekday - argWeekDay);
}
public static int[] lastMonth(Date d)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
int year = c.get(1);
int month = c.get(2) + 1;
if (month == 1) {
return new int[] { year - 1, 12 };
}
return new int[] { year, month - 1 };
}
public static int[] lastMonth(int year, int month)
{
return lastMonth(dateInstance(year, month, 1));
}
public static int[] nextMonth(Date d)
{
Calendar c = Calendar.getInstance();
c.setTime(d);
int year = c.get(1);
int month = c.get(2) + 1;
if (month == 12) {
return new int[] { year + 1, 1 };
}
return new int[] { year, month + 1 };
}
public static int[] nextMonth(int year, int month)
{
return nextMonth(dateInstance(year, month, 1));
}
public static long daysBetween(Date startDate, Date endDate)
{
return (endDate.getTime() - startDate.getTime()) / ONE_DAY_MILLIS;
}
public static Date addDays(Date d, int days)
{
if (d == null) {
return null;
}
return new Date(d.getTime() + days * ONE_DAY_MILLIS);
}
public static Date addMonths(Date d, int months)
{
if (months == 0) {
return d;
}
Calendar c = calendarInstance(d);
int day = c.get(5);
int hour = c.get(11);
int min = c.get(12);
int sec = c.get(13);
Date mydate = d;
if (months > 0) {
for (int kk = 0; kk < months; kk++)
{
int[] tmp = nextMonth(mydate);
mydate = dateInstance(tmp[0], tmp[1], 1);
}
} else {
for (int kk = 0; kk > months; kk--)
{
int[] tmp = lastMonth(mydate);
mydate = dateInstance(tmp[0], tmp[1], 1);
}
}
c = calendarInstance(mydate);
int year = c.get(1);
int month = c.get(2) + 1;
int lastDay = lastDayOfMonth(year, month);
if (day > lastDay) {
day = lastDay;
}
return dateInstance(year, month, day, hour, min, sec);
}
public static String toString(Date d, String format)
{
return new SimpleDateFormat(format).format(d);
}
public static Date toDate(String dateString, String format)
{
try
{
return new SimpleDateFormat(format).parse(dateString);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}
public static String getStrYear()
{
return new SimpleDateFormat("yyyy").format(new Date());
}
public static String getStrMonth()
{
return new SimpleDateFormat("MM").format(new Date());
}
public static String getStrDay()
{
return new SimpleDateFormat("dd").format(new Date());
}
public static String getStrYearMonthDay()
{
return new SimpleDateFormat("yyyyMMdd").format(new Date());
}
public static Date getCurDate()
{
return new Date();
}
public static BigDecimal calDays(Date beginDate, Date endDate)
{
if ((beginDate != null) && (endDate != null))
{
BigDecimal days = new BigDecimal(daysBetween(beginDate, endDate));
return days.setScale(6, 4);
}
return null;
}
public static Date truncDate(Date d, String format)
{
Calendar c = calendarInstance(d);
int year = c.get(1);
int mon = c.get(2) + 1;
int day = c.get(5);
int hour = c.get(11);
int min = c.get(12);
int sec = c.get(13);
if (format.equals("yyyy")) {
return dateInstance(year, 1, 1);
}
if (format.equals("MM")) {
return dateInstance(year, mon, 1);
}
if (format.equals("dd")) {
return dateInstance(year, mon, day);
}
if (format.equals("HH")) {
return dateInstance(year, mon, day, hour, 0, 0);
}
if (format.equals("mm")) {
return dateInstance(year, mon, day, hour, min, 0);
}
if (format.equals("ss")) {
return dateInstance(year, mon, day, hour, min, sec);
}
return d;
}
public static Date padToMax(Date d, String format)
{
Calendar c = calendarInstance(d);
int year = c.get(1);
int mon = c.get(2) + 1;
int day = c.get(5);
int hour = c.get(11);
int min = c.get(12);
int sec = c.get(13);
if (format.equals("yyyy")) {
return dateInstance(year, 12, 31, 23, 59, 59);
}
if (format.equals("MM")) {
return dateInstance(year, mon, lastDayOfMonth(year, mon), 23, 59, 59);
}
if (format.equals("dd")) {
return dateInstance(year, mon, day, 23, 59, 59);
}
if (format.equals("HH")) {
return dateInstance(year, mon, day, hour, 59, 59);
}
if (format.equals("mm")) {
return dateInstance(year, mon, day, hour, min, 59);
}
if (format.equals("ss")) {
return dateInstance(year, mon, day, hour, min, sec);
}
return d;
}
public static Timestamp date2Timestamp(Date d)
{
Calendar sysCal = Calendar.getInstance();
sysCal.setTime(d);
return new Timestamp(sysCal.getTimeInMillis());
}
public static String timestamp2String(Timestamp t)
{
String n = t.toString();
int vv = n.indexOf(".");
String bb = n.substring(0, vv);
return bb;
}
public static Date strToDate(String str, String dateformat)
throws ParseException
{
DateFormat format = new SimpleDateFormat(dateformat);
Date date = format.parse(str);
return date;
}
public static String getCurrentSysTimeNow()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
return sdf.format(date);
}
public static String getCurrentSysTimeNow(String s)
{
Date date = new Date();
s = s.length() < 4 ? "yyyy-MM-dd HH:mm:ss" : s;
SimpleDateFormat sdf = new SimpleDateFormat(s);
return sdf.format(date);
}
/**
* 得到当前月份
* @return
*/
public static String getCurrentMonthInfo(){
java.util.Calendar calendar = java.util.Calendar.getInstance();
int monthI = calendar.get(Calendar.MONTH) + 1;
return monthI + "";
}
/**
* 得到当前年份
* @return
*/
public static String getCurrentYearInfo(){
java.util.Calendar calendar = java.util.Calendar.getInstance();
int year = calendar.get(Calendar.YEAR) ;
return year + "";
}
关于时间的util类,以后方便查阅的更多相关文章
- 常用类一一时间处理相关类一一java.util.Tomezone(java.util.Calendar , java.util.Date , java.text.DateFormat)
时间处理相关类 时间是一个一维的东东.所以,我们需要一把刻度尺来区表达和度量时间.在计算机世界,我们把1970 年 1 月 1 日 00:00:00定为基准时间,每个度量单位是毫秒(1秒的千分之一). ...
- [java工具类01]__构建格式化输出日期和时间的工具类
在之前的学习中,我写过一篇关于字符串格式化的,就主要设计到了时间以及日期的各种格式化显示的设置,其主要时通过String类的fomat()方法实现的. 我们可以通过使用不同的转换符来实现格式化显示不同 ...
- Java中Date类型如何向前向后滚动时间,( 附工具类)
Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: import java.text.SimpleDateFormat; import java.util.Calendar ...
- Java日期时间实用工具类
Java日期时间实用工具类 1.Date (java.util.Date) Date(); 以当前时间构造一个Date对象 Date(long); 构造函数 ...
- jdk8 时间日期工具类(转)
package com.changfu.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import jav ...
- property_自己编写一个读取Property文件的Util类
读取property文件的Util类: 所需jar包: 编写PropertiesUtil类: package com.west.util.property; import java.io.InputS ...
- mysql取出现在的时间戳和时间时间戳转成人类看得懂的时间
mysql取出现在的时间戳和时间时间戳转成人类看得懂的时间,我们在mysql里面他封装了一个内置的时间戳转化的函数,比如我们现在的时间戳是:1458536709 ,"%Y-%m-%d&quo ...
- Problem B: 时间和日期类(III)
Problem B: 时间和日期类(III) Time Limit: 4 Sec Memory Limit: 128 MBSubmit: 2889 Solved: 1732[Submit][Sta ...
- asp.net(C#)实现功能强大的时间日期处理类完整实例
作者:smartsmile2012 字体:[增加 减小] 类型:转载 时间:2016-06-30我要评论 这篇文章主要介绍了asp.net(C#)实现功能强大的时间日期处理类,封装了针对日期与时间的各 ...
随机推荐
- AngularJS recursive(递归)
工作中我们经常要遍历多层数据,如果数据是已知层级的话,用 ng-repeat 就搞定了,要是数据深度是无限的呢,或者我们要实现一个无限层级的 tree 的时候,该怎么办? 答案是使用 ng-inclu ...
- 这是BUG吗?
百度首页的控制台里,有一段招聘信息,想必大家都知道吧,点击里面的链接地址跳到了百度招聘页面然后就发现了这个:,如果这不是BUG的话,那用户体验真是不好
- 网页优化URI(http URI scheme与data URI scheme)
网页优化的一大首要任务是减少HTTP 请求 (http request) 的次数,例如通过合并多个JS文件,合并CSS样式文件.除此之外,还有一个data URL 的密技,让我们直接把图像的内容崁入网 ...
- 【C++】String类实现
//string.h #include <iostream> using namespace std; class String; istream& operator>> ...
- SQL Server 索引介绍
数据库索引是对数据表中一个或多个列的值进行排序的结构,就像一本书的目录一样,索引提供了在行中快速查询特定行的能力 详细出处参考:http://www.jb51.net/article/30950.ht ...
- Repeater 合并单元格
前途页面: <asp:Repeater ID="rptList" runat="server" OnPreRender="rptList_Pre ...
- Visual Studio Ultimate 2013 with Update 4
Visual Studio Ultimate 2013 with Update 4 是一个先进的开发解决方案,各种规模的团队通过它均可设计和创建引人注目的应用程序,使用户兴致勃勃. Visual St ...
- C++学习之类的构造函数、析构函数
在C++的类中,都会有一个或多个构造函数.一个析构函数.一个赋值运算操作符.即使我们自己定义的类中,没有显示定义它们,编译器也会声明一个默认构造函数.一个析构函数和一个赋值运算操作符.例如: //声明 ...
- 查看某个html标签有哪些属性和事件
<html><head><script> //查看input标签有哪些属性和事件 function a() { var str = new String(" ...
- JS 用window.open()函数,父级页面如何取到子级页面的返回值?
父窗口:<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> ...