- 浏览: 563118 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
jiang2011jiang:
mybatis3源码核心类1--Configuration -
tuyf_hs:
同求 图片
zookeeper+dubbo+dubbo管理集群的简要配置[单机] -
安静听歌:
请问图片还能找的会吗?你的图片和原文的图片都挂了,,,如果有图 ...
zookeeper+dubbo+dubbo管理集群的简要配置[单机] -
ahua186186:
yngwiet 写道楼主,有一个地方不太明白,为什么要用“ge ...
ListView中getChildAt(index)的使用注意事项 -
yngwiet:
楼主,有一个地方不太明白,为什么要用“getChildAt(p ...
ListView中getChildAt(index)的使用注意事项
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
* 时间处理函数
*
* @20080509 15:50
*/
public class DateUtil {
private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String TIME_YEAR = "yyyy";
public static final String TIME_MONEN = "MM";
public static final String TIME_DAY = "dd";
public static String getDate(String interval, Date starttime, String pattern) {
Calendar temp = Calendar.getInstance(TimeZone.getDefault());
temp.setTime(starttime);
temp.add(temp.MONTH, Integer.parseInt(interval));
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(temp.getTime());
}
/**
* 将字符串类型转换为时间类型
*
* @return
*/
public static Date str2Date(String str) {
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
try {
d = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return d;
}
public static Date str2Date(String str, String pattern) {
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
d = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return d;
}
/**
* 将时间格式化
*
* @return
*/
public static Date DatePattern(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
try {
String dd = sdf.format(date);
date = str2Date(dd);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 将时间格式化
*/
public static Date DatePattern(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
String dd = sdf.format(date);
date = str2Date(dd, pattern);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
public static String date2Str(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
return sdf.format(date);
}
public static String date2Str(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 获取昨天
*
* @param date
* @return
* @throws Exception
*/
public static Date getLastDate(Date date) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
calendar.add(calendar.DATE, -1);
return str2Date(date2Str(calendar.getTime()));
}
/**
* 获取前几天
* @param date
* @return
*/
public static Date getBeforeDate(Date date,int dates) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
calendar.add(calendar.DATE, -dates);
return str2Date(date2Str(calendar.getTime()));
}
/**
* 获取上周第一天(周一)
*
* @param date
* @return
* @throws Exception
*/
public static Date getLastWeekStart(Date date) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
int i = calendar.get(calendar.DAY_OF_WEEK) - 1;
int startnum = 0;
if (i == 0) {
startnum = 7 + 6;
} else {
startnum = 7 + i - 1;
}
calendar.add(calendar.DATE, -startnum);
return str2Date(date2Str(calendar.getTime()));
}
/**
* 获取上周最后一天(周末)
*
* @param date
* @return
* @throws Exception
*/
public static Date getLastWeekEnd(Date date) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
int i = calendar.get(calendar.DAY_OF_WEEK) - 1;
int endnum = 0;
if (i == 0) {
endnum = 7;
} else {
endnum = i;
}
calendar.add(calendar.DATE, -(endnum - 1));
return str2Date(date2Str(calendar.getTime()));
}
/**
* 根据年和月得到天数
* @param num 月
* @param year 年
* @return
*/
public static int getday(int num,int year){
if(num==1 || num==3 || num==5 || num==7 || num==8 || num==10 || num==12){
return 31;
}else if(num==2){
//判断是否为闰年
if(year%400==0 || (year%4==0 && year%100!=0)){
return 29;
}else{
return 28;
}
}else{
return 30;
}
}
/*
* 计算当前日期距离下个月还有多少天
*/
public static int getdaymis(Date time){
int year = Integer.parseInt(
new SimpleDateFormat(TIME_YEAR).format(time));//年
int mm = Integer.parseInt(
new SimpleDateFormat(TIME_MONEN).format(time));//月
int dd = Integer.parseInt(
new SimpleDateFormat(TIME_DAY).format(time));//日
//获取当前年月的总天数
int sdd = getday(mm,year);
return sdd-dd;
}
/**
* 日期转秒数
* @param dateString
* @return
*/
public static long getTime(String dateString) {
long time = 0;
try {
Date ret = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ret = sdf.parse(dateString);
time = ret.getTime()/1000;
} catch (Exception e) {
}
return time;
}
/**
* 精确计算时间差,精确到日
* @param fistill 起始日期
* @param nowtime 结束日期
* @param type type为1返回年月日(如:2年3个月零5天) 否则返回总的天数
* @return
*/
public static String patienage(Date fistill,Date nowtime,Integer type){
int fyear = Integer.parseInt(
new SimpleDateFormat(TIME_YEAR).format(fistill));//起始年
int fmm = Integer.parseInt(
new SimpleDateFormat(TIME_MONEN).format(fistill));//起始月
int fdd = Integer.parseInt(
new SimpleDateFormat(TIME_DAY).format(fistill));//起始日
int nyear = Integer.parseInt(
new SimpleDateFormat(TIME_YEAR).format(nowtime));//结束年
int nmm = Integer.parseInt(
new SimpleDateFormat(TIME_MONEN).format(nowtime));//结束月
int ndd = Integer.parseInt(
new SimpleDateFormat(TIME_DAY).format(nowtime));//结束日
int cyear = nyear - fyear;
int cmm = nmm - fmm;
int cdd = ndd - fdd;
int zyear = 0;
int zmm = 0;
int zdd = 0;
int countddd = 0; //年月日累计天数
if(cdd<0){
if(cmm<0){
zyear = cyear - 1;
zmm = (cmm + 12)-1;
int dd = getday(zmm,nyear-1);
zdd = dd + cdd;
countddd = zyear*365+zmm*30+zdd;
}else if(cmm==0){
zyear = cyear - 1;
zmm = 12-1;
int dd = getday(zmm,nyear-1);
zdd = dd + cdd;
countddd = zyear*365+zmm*30+zdd;
}else{
zyear = cyear;
zmm = cmm - 1;
int dd = getday(zmm,nyear);
zdd = dd + cdd;
countddd = zyear*365+zmm*30+zdd;
}
}else if(cdd==0){
if(cmm<0){
zyear = cyear - 1;
zmm = cmm + 12;
zdd = 0;
countddd = zyear*365+zmm*30;
}else if(cmm==0){
zyear = cyear;
zmm = 0;
zdd = 0;
countddd = zyear*365+zmm*30;
}else{
zyear = cyear;
zmm = cmm;
zdd = 0;
countddd = zyear*365+zmm*30;
}
}else{
if(cmm<0){
zyear = cyear - 1;
zmm = cmm + 12;
zdd = cdd;
countddd = zyear*365+zmm*30+zdd;
}else if(cmm==0){
zyear = cyear;
zmm = 0;
zdd = cdd;
countddd = zyear*365+zmm*30+zdd;
}else{
zyear = cyear;
zmm = cmm;
zdd = cdd;
countddd = zyear*365+zmm*30+zdd;
}
}
String ptime = null;
if(zdd!=0){
if(zmm!=0){
if(zyear!=0){
ptime = zyear+"年"+zmm+"个月"+"零"+zdd+"天";
}else{
ptime = zmm+"个月"+"零"+zdd+"天";
}
}else{
if(zyear!=0){
ptime = zyear+"年"+"零"+zdd+"天";
}else{
ptime = zdd+"天";
}
}
}else{
if(zmm!=0){
if(zyear!=0){
ptime = zyear+"年"+zmm+"个月";
}else{
ptime = zmm+"个月";
}
}else{
if(zyear!=0){
ptime = zyear+"年";
}else{
ptime = null;
}
}
}
if(type==1){
return ptime; //返回年月日(如:2年3个月零5天)
}else{
return String.valueOf(countddd); //返回总天数
}
}
/**
* 得到月数
* @param year 年数差
* @param month 月数差
* @return
*/
public static int getCmm(Integer year,Integer month){
int zmm = 0;
if(month < 0){
zmm = (month + 12)+(year-1)*12;
}else if(month == 0){
zmm = year*12;
}else{
zmm = year*12+month;
}
return zmm;
}
/**
* 改更现在时间
*/
public static Date changeDate(String type, int value) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
if (type.equals("month")) {
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + value);
} else if (type.equals("date")) {
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + value);
}
return calendar.getTime();
}
/**
* 更改时间
*/
public static Date changeDate(Date date, String type, int value) {
if (date != null) {
// Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
// Calendar calendar = Calendar.
if (type.equals("month")) {
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + value);
} else if (type.equals("date")) {
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + value);
} else if (type.endsWith("year")) {
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + value);
}
return calendar.getTime();
}
return null;
}
/**
* haoxw 比较时间是否在这两个时间点之间
*
* @param time1
* @param time2
* @return
*/
public static boolean checkTime(String time1, String time2) {
Calendar calendar = Calendar.getInstance();
Date date1 = calendar.getTime();
Date date11 = DateUtil.str2Date(DateUtil.date2Str(date1, "yyyy-MM-dd") + " " + time1);// 起始时间
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date date2 = c.getTime();
Date date22 = DateUtil.str2Date(DateUtil.date2Str(date2, "yyyy-MM-dd") + " " + time2);// 终止时间
Calendar scalendar = Calendar.getInstance();
scalendar.setTime(date11);// 起始时间
Calendar ecalendar = Calendar.getInstance();
ecalendar.setTime(date22);// 终止时间
Calendar calendarnow = Calendar.getInstance();
if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
return true;
} else {
return false;
}
}
/**
* haoxw 比较时间是否在这两个时间点之间
*
* @param date11
* @param date22
* @return
*/
public static boolean checkTime(Date date11, Date date22) {
Calendar scalendar = Calendar.getInstance();
scalendar.setTime(date11);// 起始时间
Calendar ecalendar = Calendar.getInstance();
ecalendar.setTime(date22);// 终止时间
Calendar calendarnow = Calendar.getInstance();
if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
return true;
} else {
return false;
}
}
public static boolean checkDate(String date1, String date2) {
Date date11 = DateUtil.str2Date(date1, "yyyy-MM-dd HH:mm:ss");// 起始时间
Date date22 = DateUtil.str2Date(date2, "yyyy-MM-dd HH:mm:ss");// 终止时间
Calendar scalendar = Calendar.getInstance();
scalendar.setTime(date11);// 起始时间
Calendar ecalendar = Calendar.getInstance();
ecalendar.setTime(date22);// 终止时间
Calendar calendarnow = Calendar.getInstance();
System.out.println(date11.toString());
System.out.println(date22.toString());
System.out.println(scalendar.toString());
System.out.println(ecalendar.toString());
System.out.println(calendarnow.toString());
if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
return true;
} else {
return false;
}
}
/**
* 获取interval天之前的日期
*
* @param interval
* @param starttime
* @param pattern
* @return
*/
public static Date getIntervalDate(String interval, Date starttime, String pattern) {
Calendar temp = Calendar.getInstance();
temp.setTime(starttime);
temp.add(temp.DATE, Integer.parseInt(interval));
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String shijian = sdf.format(temp.getTime());
return str2Date(shijian);
}
public static Date formatDate(Date date){
SimpleDateFormat bartDateFormat =
new SimpleDateFormat("yyyy-MM-dd");
System.out.println(bartDateFormat.format(date));
SimpleDateFormat bartDateFormat1 =new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = bartDateFormat1.parse(bartDateFormat.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date.getTime());
return date;
}
public static void main(String arf[]) {
/*String time1 = "2009-05-07 19:20:00";
String time2 = "2009-05-08 19:30:00";
DateUtil d = new DateUtil();
System.out.println(d.checkDate(time1, time2));
System.out.println(d.date2Str(new Date()));*/
//System.out.println(d.getIntervalDate("-3", new Date(), DEFAULT_PATTERN));
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
System.out.println(calendar.toString());
System.out.println(DateUtil.str2Date("20090731","yyyyMMdd"));
System.out.println(DateUtil.getBeforeDate(new Date(),2 ));
System.out.println(DateUtil.DatePattern(new Date()));
SimpleDateFormat bartDateFormat =
new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
System.out.println("date;"+bartDateFormat.format(date));
SimpleDateFormat bartDateFormat1 =new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = bartDateFormat1.parse(bartDateFormat.format(date));
System.out.println("日期:"+date1);
} catch (ParseException e) {
e.printStackTrace();
}
}
转自:http://lucien-zzy.iteye.com/blog/2009811
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
* 时间处理函数
*
* @20080509 15:50
*/
public class DateUtil {
private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String TIME_YEAR = "yyyy";
public static final String TIME_MONEN = "MM";
public static final String TIME_DAY = "dd";
public static String getDate(String interval, Date starttime, String pattern) {
Calendar temp = Calendar.getInstance(TimeZone.getDefault());
temp.setTime(starttime);
temp.add(temp.MONTH, Integer.parseInt(interval));
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(temp.getTime());
}
/**
* 将字符串类型转换为时间类型
*
* @return
*/
public static Date str2Date(String str) {
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
try {
d = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return d;
}
public static Date str2Date(String str, String pattern) {
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
d = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return d;
}
/**
* 将时间格式化
*
* @return
*/
public static Date DatePattern(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
try {
String dd = sdf.format(date);
date = str2Date(dd);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 将时间格式化
*/
public static Date DatePattern(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
String dd = sdf.format(date);
date = str2Date(dd, pattern);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
public static String date2Str(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
return sdf.format(date);
}
public static String date2Str(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 获取昨天
*
* @param date
* @return
* @throws Exception
*/
public static Date getLastDate(Date date) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
calendar.add(calendar.DATE, -1);
return str2Date(date2Str(calendar.getTime()));
}
/**
* 获取前几天
* @param date
* @return
*/
public static Date getBeforeDate(Date date,int dates) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
calendar.add(calendar.DATE, -dates);
return str2Date(date2Str(calendar.getTime()));
}
/**
* 获取上周第一天(周一)
*
* @param date
* @return
* @throws Exception
*/
public static Date getLastWeekStart(Date date) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
int i = calendar.get(calendar.DAY_OF_WEEK) - 1;
int startnum = 0;
if (i == 0) {
startnum = 7 + 6;
} else {
startnum = 7 + i - 1;
}
calendar.add(calendar.DATE, -startnum);
return str2Date(date2Str(calendar.getTime()));
}
/**
* 获取上周最后一天(周末)
*
* @param date
* @return
* @throws Exception
*/
public static Date getLastWeekEnd(Date date) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
int i = calendar.get(calendar.DAY_OF_WEEK) - 1;
int endnum = 0;
if (i == 0) {
endnum = 7;
} else {
endnum = i;
}
calendar.add(calendar.DATE, -(endnum - 1));
return str2Date(date2Str(calendar.getTime()));
}
/**
* 根据年和月得到天数
* @param num 月
* @param year 年
* @return
*/
public static int getday(int num,int year){
if(num==1 || num==3 || num==5 || num==7 || num==8 || num==10 || num==12){
return 31;
}else if(num==2){
//判断是否为闰年
if(year%400==0 || (year%4==0 && year%100!=0)){
return 29;
}else{
return 28;
}
}else{
return 30;
}
}
/*
* 计算当前日期距离下个月还有多少天
*/
public static int getdaymis(Date time){
int year = Integer.parseInt(
new SimpleDateFormat(TIME_YEAR).format(time));//年
int mm = Integer.parseInt(
new SimpleDateFormat(TIME_MONEN).format(time));//月
int dd = Integer.parseInt(
new SimpleDateFormat(TIME_DAY).format(time));//日
//获取当前年月的总天数
int sdd = getday(mm,year);
return sdd-dd;
}
/**
* 日期转秒数
* @param dateString
* @return
*/
public static long getTime(String dateString) {
long time = 0;
try {
Date ret = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ret = sdf.parse(dateString);
time = ret.getTime()/1000;
} catch (Exception e) {
}
return time;
}
/**
* 精确计算时间差,精确到日
* @param fistill 起始日期
* @param nowtime 结束日期
* @param type type为1返回年月日(如:2年3个月零5天) 否则返回总的天数
* @return
*/
public static String patienage(Date fistill,Date nowtime,Integer type){
int fyear = Integer.parseInt(
new SimpleDateFormat(TIME_YEAR).format(fistill));//起始年
int fmm = Integer.parseInt(
new SimpleDateFormat(TIME_MONEN).format(fistill));//起始月
int fdd = Integer.parseInt(
new SimpleDateFormat(TIME_DAY).format(fistill));//起始日
int nyear = Integer.parseInt(
new SimpleDateFormat(TIME_YEAR).format(nowtime));//结束年
int nmm = Integer.parseInt(
new SimpleDateFormat(TIME_MONEN).format(nowtime));//结束月
int ndd = Integer.parseInt(
new SimpleDateFormat(TIME_DAY).format(nowtime));//结束日
int cyear = nyear - fyear;
int cmm = nmm - fmm;
int cdd = ndd - fdd;
int zyear = 0;
int zmm = 0;
int zdd = 0;
int countddd = 0; //年月日累计天数
if(cdd<0){
if(cmm<0){
zyear = cyear - 1;
zmm = (cmm + 12)-1;
int dd = getday(zmm,nyear-1);
zdd = dd + cdd;
countddd = zyear*365+zmm*30+zdd;
}else if(cmm==0){
zyear = cyear - 1;
zmm = 12-1;
int dd = getday(zmm,nyear-1);
zdd = dd + cdd;
countddd = zyear*365+zmm*30+zdd;
}else{
zyear = cyear;
zmm = cmm - 1;
int dd = getday(zmm,nyear);
zdd = dd + cdd;
countddd = zyear*365+zmm*30+zdd;
}
}else if(cdd==0){
if(cmm<0){
zyear = cyear - 1;
zmm = cmm + 12;
zdd = 0;
countddd = zyear*365+zmm*30;
}else if(cmm==0){
zyear = cyear;
zmm = 0;
zdd = 0;
countddd = zyear*365+zmm*30;
}else{
zyear = cyear;
zmm = cmm;
zdd = 0;
countddd = zyear*365+zmm*30;
}
}else{
if(cmm<0){
zyear = cyear - 1;
zmm = cmm + 12;
zdd = cdd;
countddd = zyear*365+zmm*30+zdd;
}else if(cmm==0){
zyear = cyear;
zmm = 0;
zdd = cdd;
countddd = zyear*365+zmm*30+zdd;
}else{
zyear = cyear;
zmm = cmm;
zdd = cdd;
countddd = zyear*365+zmm*30+zdd;
}
}
String ptime = null;
if(zdd!=0){
if(zmm!=0){
if(zyear!=0){
ptime = zyear+"年"+zmm+"个月"+"零"+zdd+"天";
}else{
ptime = zmm+"个月"+"零"+zdd+"天";
}
}else{
if(zyear!=0){
ptime = zyear+"年"+"零"+zdd+"天";
}else{
ptime = zdd+"天";
}
}
}else{
if(zmm!=0){
if(zyear!=0){
ptime = zyear+"年"+zmm+"个月";
}else{
ptime = zmm+"个月";
}
}else{
if(zyear!=0){
ptime = zyear+"年";
}else{
ptime = null;
}
}
}
if(type==1){
return ptime; //返回年月日(如:2年3个月零5天)
}else{
return String.valueOf(countddd); //返回总天数
}
}
/**
* 得到月数
* @param year 年数差
* @param month 月数差
* @return
*/
public static int getCmm(Integer year,Integer month){
int zmm = 0;
if(month < 0){
zmm = (month + 12)+(year-1)*12;
}else if(month == 0){
zmm = year*12;
}else{
zmm = year*12+month;
}
return zmm;
}
/**
* 改更现在时间
*/
public static Date changeDate(String type, int value) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
if (type.equals("month")) {
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + value);
} else if (type.equals("date")) {
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + value);
}
return calendar.getTime();
}
/**
* 更改时间
*/
public static Date changeDate(Date date, String type, int value) {
if (date != null) {
// Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
// Calendar calendar = Calendar.
if (type.equals("month")) {
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + value);
} else if (type.equals("date")) {
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + value);
} else if (type.endsWith("year")) {
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + value);
}
return calendar.getTime();
}
return null;
}
/**
* haoxw 比较时间是否在这两个时间点之间
*
* @param time1
* @param time2
* @return
*/
public static boolean checkTime(String time1, String time2) {
Calendar calendar = Calendar.getInstance();
Date date1 = calendar.getTime();
Date date11 = DateUtil.str2Date(DateUtil.date2Str(date1, "yyyy-MM-dd") + " " + time1);// 起始时间
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date date2 = c.getTime();
Date date22 = DateUtil.str2Date(DateUtil.date2Str(date2, "yyyy-MM-dd") + " " + time2);// 终止时间
Calendar scalendar = Calendar.getInstance();
scalendar.setTime(date11);// 起始时间
Calendar ecalendar = Calendar.getInstance();
ecalendar.setTime(date22);// 终止时间
Calendar calendarnow = Calendar.getInstance();
if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
return true;
} else {
return false;
}
}
/**
* haoxw 比较时间是否在这两个时间点之间
*
* @param date11
* @param date22
* @return
*/
public static boolean checkTime(Date date11, Date date22) {
Calendar scalendar = Calendar.getInstance();
scalendar.setTime(date11);// 起始时间
Calendar ecalendar = Calendar.getInstance();
ecalendar.setTime(date22);// 终止时间
Calendar calendarnow = Calendar.getInstance();
if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
return true;
} else {
return false;
}
}
public static boolean checkDate(String date1, String date2) {
Date date11 = DateUtil.str2Date(date1, "yyyy-MM-dd HH:mm:ss");// 起始时间
Date date22 = DateUtil.str2Date(date2, "yyyy-MM-dd HH:mm:ss");// 终止时间
Calendar scalendar = Calendar.getInstance();
scalendar.setTime(date11);// 起始时间
Calendar ecalendar = Calendar.getInstance();
ecalendar.setTime(date22);// 终止时间
Calendar calendarnow = Calendar.getInstance();
System.out.println(date11.toString());
System.out.println(date22.toString());
System.out.println(scalendar.toString());
System.out.println(ecalendar.toString());
System.out.println(calendarnow.toString());
if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
return true;
} else {
return false;
}
}
/**
* 获取interval天之前的日期
*
* @param interval
* @param starttime
* @param pattern
* @return
*/
public static Date getIntervalDate(String interval, Date starttime, String pattern) {
Calendar temp = Calendar.getInstance();
temp.setTime(starttime);
temp.add(temp.DATE, Integer.parseInt(interval));
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String shijian = sdf.format(temp.getTime());
return str2Date(shijian);
}
public static Date formatDate(Date date){
SimpleDateFormat bartDateFormat =
new SimpleDateFormat("yyyy-MM-dd");
System.out.println(bartDateFormat.format(date));
SimpleDateFormat bartDateFormat1 =new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = bartDateFormat1.parse(bartDateFormat.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date.getTime());
return date;
}
public static void main(String arf[]) {
/*String time1 = "2009-05-07 19:20:00";
String time2 = "2009-05-08 19:30:00";
DateUtil d = new DateUtil();
System.out.println(d.checkDate(time1, time2));
System.out.println(d.date2Str(new Date()));*/
//System.out.println(d.getIntervalDate("-3", new Date(), DEFAULT_PATTERN));
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
System.out.println(calendar.toString());
System.out.println(DateUtil.str2Date("20090731","yyyyMMdd"));
System.out.println(DateUtil.getBeforeDate(new Date(),2 ));
System.out.println(DateUtil.DatePattern(new Date()));
SimpleDateFormat bartDateFormat =
new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
System.out.println("date;"+bartDateFormat.format(date));
SimpleDateFormat bartDateFormat1 =new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = bartDateFormat1.parse(bartDateFormat.format(date));
System.out.println("日期:"+date1);
} catch (ParseException e) {
e.printStackTrace();
}
}
转自:http://lucien-zzy.iteye.com/blog/2009811
发表评论
-
shiro落地的设计复杂度(最后总结)
2018-06-19 17:22 581经过1周的源码研究,终于对shiro的原理有了深刻的理解,基于 ... -
shiro login成功后保存了哪些数据
2018-06-19 17:05 1476shiro login成功后 保存了Principals 和 ... -
shiro 会话原理分析
2018-06-19 12:40 15951、从哪里获取sessionid每次请求都会尝试获取ses ... -
shiro内部原理分析
2018-06-15 17:07 2413一句话总结:会话域Context一路收集principal ... -
Zookeeper入门-001 源码环境搭建
2018-03-15 11:47 9061.到github下载源码:https://github.c ... -
dubbo服务治理之路由规则研究
2018-01-31 15:50 22111.今天没太多事情,挤 ... -
shiro SecurityUtils.getSubject()深度分析
2018-01-12 17:38 489411.总的来说,SecurityUtils.getSubject ... -
@Async核心实现1 --------AsyncExecutionAspectSupport
2017-12-27 10:34 1983基本原理: 通过spring的扩展接口AbstractBea ... -
从零开始玩转JMX(1):简介和 Standard MBean
2017-08-23 15:20 0http://www.importnew.com/22299. ... -
java基础回顾
2017-08-15 11:21 0http://www.cnblogs.com/skywang1 ... -
mybatis-generator-maven-plugin 插件扩展 增加自定义方法
2017-08-10 16:50 0https://my.oschina.net/alexgaoy ... -
解决了DeferredResult请求长时间占用数据库连接的问题
2017-08-04 09:55 2359最近看了看开源项目appllo配置中心的源码,发现一个很有意思 ... -
httpclient发送webservice
2017-05-03 23:25 0http://aperise.iteye.com/blog/2 ... -
与大师面对面交流:Chris Richardson 来华布道微服务架构
2016-11-28 21:28 825http://www.daocloud.io/microser ... -
eclipse反编译
2016-11-23 20:48 0http://jingyan.baidu.com/articl ... -
spring security
2016-09-12 19:28 0http://www.importnew.com/5641.h ... -
浅析JPA中EntityManager无法remove entity的问题
2016-07-18 21:50 0http://rickqin.blog.51cto.com/1 ... -
Permission Denied(publickey) 解决
2016-07-14 19:18 27821.生成公钥和私钥放到C:\Users\itservice\. ... -
权限管理系统
2016-04-18 13:29 0http://git.oschina.net/ketayao/ ... -
Java工程师成神之路--面试必须复习的基础
2015-12-28 17:20 0http://www.open-open.com/news/v ...
相关推荐
资源名称:DateUtil 日期时间转换工具类 内容概要:日期时间转换工具类,包括基本的Date类型,String类型,TimeStamp类型,LocalDateTime类型,LocalDate类型之间的互相转换,还提供了许多与时间获取,时间计算有关...
"Android日期工具类DateUtil详解" Android开发中日期工具类DateUtil是Android开发中一个非常重要的工具类,它提供了许多有用的方法来处理日期和时间相关的操作。今天,我们将详细介绍DateUtil工具类的实现和使用。 ...
在Java编程中,DateUtil是一个常见的工具类,用于处理日期和时间相关的操作。这个类通常包含了一系列静态方法,便于开发者进行日期格式化、日期比较、日期计算等常见任务。下面我们将详细探讨DateUtil中的关键知识点...
在Java编程中,DateUtil工具类是用于处理和操作日期时间的一个常见实用程序类。它提供了许多方便的方法,使得开发者可以轻松地进行日期格式化、转换以及比较等操作。在这个"dateUtil工具类"中,我们可以看到核心功能...
在Java编程领域,DateUtil工具类是开发人员经常会用到的一种辅助类,它提供了一系列方便的方法来处理日期和时间。这个被称为"史上最全面DateUtil工具类,没有之一"的资源,显然包含了处理日期和时间的各种功能,使得...
DateUtil 日期工具类
public static final String YYYYMMDDHHMMSS = "yyyy/MM/dd HH:mm:ss";
DateUtil:时间处理工具类 DBConnection:jdbc工具类 FileOperater:文件处理工具类 包括 读取文本文件,写出文本文件, 大文件切分,文件下载,文件或文件夹比较,文件或文件夹遍历筛选 ...... HttpClientCard...
Java中的DateUtil时间工具类是开发者在处理日期和时间时常用的一个自定义工具类。它通常包含了一系列静态方法,用于简化Java内置的日期和时间API的使用,提高代码的可读性和可维护性。在实际开发中,由于Java 8之前...
2. **字符串处理工具类**:如`StringUtil`,提供字符串的格式化、检查空值、拼接、分割等操作,避免在代码中反复进行这些基础操作。 3. **日期时间工具类**:例如`DateUtil`,可以方便地进行日期时间的转换,如将...
[工具类] XML工具类2 .java.txt [工具类] 测试Mysql的最大连接数 .java.txt [工具类] 读取、打印输出、保存xml .java.txt [工具类] 分页split_page.jsp .jsp.txt [工具类] 获得汉字拼音首字母的java工具类.java.txt ...
在Java编程语言中,`DateUtil`通常是一个自定义的工具类,用于处理与日期和时间相关的操作。这个工具类可以极大地简化日期处理任务,提供比Java内置的`java.util.Date`和`java.time`包更友好的API。在本文中,我们将...
2.比较传入时间与当前时间前一天的大小,传入时间在后返回true,传入时间在前返回false isWithinOneDay(String dateStr); 3.String转Date stringToDate(String dateStr, String format); 4.Date转String date...
dateUtil js工具类
[工具类] XML工具类2 .java.txt [工具类] 测试Mysql的最大连接数 .java.txt [工具类] 读取、打印输出、保存xml .java.txt [工具类] 分页split_page.jsp .jsp.txt [工具类] 获得汉字拼音首字母的java工具类.java.txt ...
4. ** relativedelta**:dateutil.relativedelta类允许我们进行相对日期运算,例如"3个月后"或"5天前",这对于计算未来的日期或对日期进行增量修改非常方便。 5. **Weekdays and Week Numbers**:可以轻松获取一周...
快速日期格式化类 ,线程安全的 包括:获取 DateUtil.STYLE格式的日期 字符转日期 日期转字符 字符日期从src_format改为dest_format 返回当前系统日期时间等
这里我们关注的是`DateUtil`工具类,它专门用于处理日期转换格式的操作。`DateUtil`工具类通常包含了对日期进行格式化、解析、比较等常用功能,使得在Android项目中处理日期变得更加便捷。 在Java中,日期对象主要...
D:\002 我的工具类\003 日期\DateUtil.java D:\002 我的工具类\004 加密 D:\002 我的工具类\004 加密\DESUtils.java D:\002 我的工具类\004 加密\PasswordHash.java D:\002 我的工具类\005 随机数 D:\002 我的工具类...
JS日期工具类,方便大家的使用。后期会持续更新.......