Java-DateHandler工具类
import java.util.*;
import java.text.*;
public class DateHandler {
public DateHandler() {
}
public static int openDay=5;
private String iDate="";
private int iYear;
private int iMonth;
private int iDay;
// iDateTime = 2002-01-01 23:23:23
public void setDate(String iDateTime){
this.iDate=iDateTime.substring(0,10);
}
public String getDate(){
return this.iDate;
}
public int getYear(){
iYear=Integer.parseInt(iDate.substring(0,4));
return iYear;
}
public int getMonth(){
iMonth=Integer.parseInt(iDate.substring(5,7));
return iMonth;
}
public int getDay(){
iDay=Integer.parseInt(iDate.substring(8,10));
return iDay;
}
public static String subDate(String date){
return date.substring(0,10);
}
/**
* 计算是否是季度末
* @param date
* @return
*/
public static boolean isSeason(String date){
int getMonth = Integer.parseInt(date.substring(5,7));
boolean sign = false;
if (getMonth==3)
sign = true;
if (getMonth==6)
sign = true;
if (getMonth==9)
sign = true;
if (getMonth==12)
sign = true;
return sign;
}
/**
* 计算从现在开始几天后的时间
* @param afterDay
* @return
*/
public static String getDateFromNow(int afterDay){
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+afterDay);
date = calendar.getTime();
return df.format(date);
}
/**
* 带格式
* @param afterDay
* @param format_string
* @return
*/
public static String getDateFromNow(int afterDay, String format_string)
{
Calendar calendar = Calendar.getInstance();
Date date = null;
DateFormat df = new SimpleDateFormat(format_string);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + afterDay);
date = calendar.getTime();
return df.format(date);
}
/**
* 得到当前时间,用于文件名,没有特殊字符,使用yyyyMMddHHmmss格式
* @param afterDay
* @return
* by tim
*/
public static String getNowForFileName(int afterDay){
GregorianCalendar calendar = new GregorianCalendar();
// Date date = calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+afterDay);
Date date = calendar.getTime();
return df.format(date);
}
//==============================================================================
//比较日期,与现在-N天的日期对比 -1 0 1
//==============================================================================
public int getDateCompare(String limitDate,int afterDay){
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime();
calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+afterDay);
date = calendar.getTime();//date是新来的天数,跟今天相比的天数
iDate=limitDate;
calendar.set(getYear(),getMonth()-1,getDay());
Date dateLimit = calendar.getTime();
return dateLimit.compareTo(date);
}
//==============================================================================
//比较日期,与现在的日期对比
//==============================================================================
public int getDateCompare(String limitDate){
iDate=limitDate;
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(getYear(),getMonth()-1,getDay());
Date date = calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate = new Date();
return date.compareTo(nowDate);
}
//==============================================================================
//比较日期,与现在的日期对比 得到天数
//==============================================================================
public long getLongCompare(String limitDate){
iDate=limitDate;
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(getYear(),getMonth()-1,getDay());
Date date = calendar.getTime();
long datePP=date.getTime();
Date nowDate = new Date();
long dateNow = nowDate.getTime();
return ((dateNow-datePP)/(24*60*60*1000));
}
//==============================================================================
//比较日期,与现在的日期对比 得到信息
//==============================================================================
public String getStringCompare(String limitDate,int openDay){
iDate=limitDate;
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(getYear(),getMonth()-1,getDay());
Date date = calendar.getTime();
long datePP=date.getTime();
Date nowDate = new Date();
long dateNow = nowDate.getTime();
long overDay = ((dateNow-datePP)/(24*60*60*1000));
String info="";
return info;
}
//==============================================================================
//比较日期,与现在的日期对比 得到信息
//==============================================================================
public String getPicCompare(String limitDate,int openDay){
iDate=limitDate;
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(getYear(),getMonth()-1,getDay());
Date date = calendar.getTime();
long datePP=date.getTime();
Date nowDate = new Date();
long dateNow = nowDate.getTime();
long overDay = ((dateNow-datePP)/(24*60*60*1000));
String info="";
if (overDay>0){
info="plaint1.gif";
}
if (overDay==0){
info="plaint2.gif";
}
if (overDay<0&&overDay>=-openDay){
info="plaint2.gif";
}
if (overDay<-openDay){
info="plaint3.gif";
}
if (overDay<-150){
info="plaint3.gif";
}
return info;
}
/**
* method diffdate 计算两个日期间相隔的日子
* @param beforDate 格式:2005-06-20
* @param afterDate 格式:2005-06-21
* @return
*/
public static int diffDate(String beforeDate,String afterDate){
String[] tt = beforeDate.split("-");
Date firstDate = new Date(Integer.parseInt(tt[0]),Integer.parseInt(tt[1])-1,Integer.parseInt(tt[2]));
tt = afterDate.split("-");
Date nextDate = new Date(Integer.parseInt(tt[0]),Integer.parseInt(tt[1])-1,Integer.parseInt(tt[2]));
return (int)(nextDate.getTime()-firstDate.getTime())/(24*60*60*1000);
}
/**
* 获取今天的日期的字符串
* @return
*/
public static String getToday(){
Calendar cld=Calendar.getInstance();
java.util.Date date=new Date();
cld.setTime(date);
int intMon=cld.get(Calendar.MONTH)+1;
int intDay=cld.get(Calendar.DAY_OF_MONTH);
String mons=String.valueOf(intMon);
String days=String.valueOf(intDay);
if(intMon<10)
mons="0"+String.valueOf(intMon);
if(intDay<10)
days="0"+String.valueOf(intDay);
return String.valueOf(cld.get(Calendar.YEAR))+"-"+mons+"-"+days;
}
/**
* 获取当前月份
* @return 返回字符串 格式:两位数
*/
public static String getCurrentMonth(){
String strmonth = null;
Calendar cld = Calendar.getInstance();
java.util.Date date = new Date();
cld.setTime(date);
int intMon=cld.get(Calendar.MONTH) + 1;
if(intMon<10)
strmonth = "0" + String.valueOf(intMon);
else
strmonth = String.valueOf(intMon);
date = null;
return strmonth;
}
// public static String getCurrMonth()
// {
// Calendar cld=Calendar.getInstance();
// java.util.Date date=new Date();
// cld.setTime(date);
// int intMon=cld.get(Calendar.MONTH)+1;
//
// return String.valueOf(intMon).toString();
// }
/**
* 获取昨天的日期的字符串
*/
public static String getYestoday(){
Calendar cld = Calendar.getInstance();
java.util.Date date = new Date();
cld.setTime(date);
cld.add(Calendar.DATE,-1);
int intMon = cld.get(Calendar.MONTH)+1;
int intDay = cld.get(Calendar.DAY_OF_MONTH);
String mons = String.valueOf(intMon);
String days = String.valueOf(intDay);
if(intMon < 10)
mons="0" + String.valueOf(intMon);
if(intDay < 10)
days = "0" + String.valueOf(intDay);
return String.valueOf(cld.get(Calendar.YEAR)) + "-" + mons + "-" + days;
}
/**
* 此函数用来计算员工的工作天数,如在使用期和离职期该月份的工作日
* 输入(离职日期,-1)可得该月工作天数 时间以2002-12-14为准
* 输入(入司时间,1)可的该月工作天数
*/
public static int getWorkDay(String date , int sign){
int month=0;
int week=0;
int workDay=0;
Calendar rightNow = Calendar.getInstance();
DateHandler dateOver=new DateHandler();
dateOver.setDate(date);
rightNow.set(rightNow.YEAR,dateOver.getYear());
rightNow.set(rightNow.MONTH,dateOver.getMonth()-1);
rightNow.set(rightNow.DATE,dateOver.getDay());
month = rightNow.get(rightNow.MONTH);
while(rightNow.get(rightNow.MONTH)==month){
week=rightNow.get(Calendar.DAY_OF_WEEK);
if (week==1||week==7){
}else{
workDay++;
System.out.println(rightNow.get(rightNow.DATE));
}
rightNow.add(rightNow.DATE,sign);
}
return workDay;
}
public static void main(String args[]){
System.out.println(DateHandler.isSeason("2002-03-02"));
// String cc ="100.123.342";
// System.out.println(cc.indexOf(".",3));
//
// StringTokenizer st=new StringTokenizer(cc,".");
//
// if (st.countTokens()!=2) {
//
// String state = st.nextToken();
// String event = st.nextToken();
// System.out.println(""+event);
String strDate = DateHandler.getDateFromNow(0,"yyyy-MM-dd HH:mm:ss");
System.out.println("date:" + strDate);
System.out.println("15:" + strDate.substring(0,16));
Date firstDate = new Date(2006,11,14,18,3,0);
Date nextDate = new Date(2006,11,15,18,2,0);
System.out.println("date's num: " + (int)(nextDate.getTime()-firstDate.getTime())/(24*60*60*1000));
// }
//System.out.println(DateHandler.getWorkDay("2002-11-14",-1));
}
}
Java-DateHandler工具类的更多相关文章
- Java Properties工具类详解
1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...
- Java json工具类,jackson工具类,ObjectMapper工具类
Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...
- Java日期工具类,Java时间工具类,Java时间格式化
Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...
- Java并发工具类 - CountDownLatch
Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...
- MinerUtil.java 爬虫工具类
MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...
- MinerDB.java 数据库工具类
MinerDB.java 数据库工具类 package com.iteye.injavawetrust.miner; import java.sql.Connection; import java.s ...
- 小记Java时间工具类
小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...
- Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie
Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...
- UrlUtils工具类,Java URL工具类,Java URL链接工具类
UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...
- java日期工具类DateUtil-续一
上篇文章中,我为大家分享了下DateUtil第一版源码,但就如同文章中所说,我发现了还存在不完善的地方,所以我又做了优化和扩展. 更新日志: 1.修正当字符串日期风格为MM-dd或yyyy-MM时,若 ...
随机推荐
- SQL用法三(游标和Fetch)
/一般情况下,我们用SELECT这些查询语句时,都是针对的一行记录而言,如果要在查询分析器中对多行记录(即记录集)进行读取操作时,则需要使用到游标或WHILE等循环/以下内容摘自http://www. ...
- ubuntu 设置静态ip,但显示scope global secondary ens33
设置静态ip 修改 /etc/network/interfaces 文件 # The loopback network interface auto lo iface lo inet loopback ...
- SQLite基础-8.子句(二)
目录 SQLite子句(二) 1. GROUP BY子句 2. HAVING子句 3. LIMIT 子句 4. IF EXISTS 和 IF NOT EXISTS 子句 SQLite子句(二) 1. ...
- sql server 中 like 中文不匹配问题解决就这么简单
原文:sql server 中 like 中文不匹配问题解决就这么简单 MS-SQL Server select * from Book where BookName like'%C语言%' 在SQ ...
- TreeMap——实现comparable接口并重写CompareTo方法
public class TreeMapTest { public static void main(String[] args) { Map<Student,Integer> stude ...
- JS基础_if练习一
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 创建json对象
jQuery创建json对象 方法二: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- vue项目中实现图片懒加载的方法
对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面内未出现在可视区域内的图片先不做加载, 等到滚动到可视区域后再去加载.这样子对于页面加载性能上会有很大的提升,也提高了用户体验. 实 ...
- Centos7搭建Docker部署LNMP
1.首先呢先更新yum源 yum update 2.1.安装docker存储库 yum install -y yum-utils \ device-mapper-persistent-data \ l ...
- 第四篇.python的基础
目录 第四篇.python基础01 1. 变量 2. 常量 3. python变量内存管理 4. 变量的三个特征 5. 花式赋值 6. 注释 7. 数据类型基础 8. 数字类型 9. 字符串类型 10 ...