DateFormat.java 14.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
package com.espeed.reading.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/*
 * 程序名称:    	EspeedMail_时速邮箱
 * 程序版本:    	V1.0
 * 作    者:    	深圳市科飞时速网络技术有限公司(0755-88843776)
 * 版权所有:    	深圳市科飞时速网络技术有限公司
 * 技术支持:    	Tech@21gmail.com
 * 单元名称:    日期格式化工具类
 * 开始时间:    	2013.09.26
 * 程 序 员:    	谢勇
 * 最后修改:    
 * 备    注:		如需修改请通知程序员    
 */
public class DateFormat {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
	private static SimpleDateFormat sdf2 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	/** 将字符串转换日期 */
	public static Date dateToString(String str) {

		Date dateNum = null;
		try {
			dateNum = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateNum;
	}

	/** 将字符串转换日期 */
	public static Date dateToString2(String str) {
		Date dateNum = null;
		try {
			dateNum = sdf2.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateNum;
	}

	/** 将日期转换为字符串 */
	public static String simpleDataFormat(Date date) {// yy必须小写,MM是月份,mm是分;HH是24小时制,而hh是12小时制
		String str = sdf.format(date);
		return str;
	}

	/** 给定一个日期型字符串,返回加减n天后的日期型字符串 */
	public static String nDaysAfterOneDateString(String basicDate, int n) {
		// SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
		Date tmpDate = null;
		try {
			tmpDate = sdf2.parse(basicDate);
		} catch (Exception e) {
			// 日期型字符串格式错误
		}
		long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)
				* (24 * 60 * 60 * 1000);
		tmpDate.setTime(nDay);

		return sdf2.format(tmpDate);
	}

	public static String getDateAfter(int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(new Date());
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return sdf2.format(now.getTime());
	}

	/** 给定一个日期,返回加减n天后的日期 */
	public static Date nDaysAfterOneDate(Date basicDate, int n) {
		long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)
				* (24 * 60 * 60 * 1000);
		basicDate.setTime(nDay);

		return basicDate;
	}

	// 获取当前时间加减后的日期
	public static String lastDays(int n) {
		String resultdate = "";
		try {
			// 获取几天之前的时间
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd"); // 字符串转换
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(new Date().getTime());
			c.add(Calendar.DATE, n);// 多少天后的日期
			Date date = new Date(c.getTimeInMillis()); // 将c转换成Date
			resultdate = formatDate.format(date);
		} catch (Exception e) {
			resultdate = "";
			e.printStackTrace();
		}
		return resultdate;
	}

	/** 判断今天星期几 */
	public static int TodayOfWeek() {
		int todayofweek = 0;
		try {
			// 计算此日期是一周中的哪一天
			todayofweek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return todayofweek;
	}

	/** 判断今天本月第几天 */
	public static int TodayOfMonth() {
		int todayofmonth = 0;
		try {
			todayofmonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH) - 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return todayofmonth;
	}

	// 最近三个月
	public static int FristdayThreeMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
		String fristdayThreeMonth = null;
		int dd = 0;

		Calendar calendar;
		try {
			Date date2 = new Date();
			calendar = Calendar.getInstance();
			calendar.add(Calendar.MONTH, -3);
			calendar.getInstance().set(Calendar.DAY_OF_MONTH, 1);
			fristdayThreeMonth = df.format(calendar.getTime());
			Date date1 = sdf.parse(fristdayThreeMonth);
			dd = (int) ((date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dd;
	}

	// 上月第一天
	public static String FristdayLasttMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String fristdaylasttmonth = null;
		Calendar calendar;
		try {
			calendar = Calendar.getInstance();
			calendar.add(Calendar.MONTH, -1);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			fristdaylasttmonth = df.format(calendar.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fristdaylasttmonth;
	}

	// 上月最后一天
	public static String LastdayLastMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String lastdaylastmonth = null;
		Calendar calendar;
		try {
			calendar = Calendar.getInstance();
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.add(Calendar.DATE, -1);
			lastdaylastmonth = df.format(calendar.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		// //获取上个月最后一天的方法,这个稍微要变通一下,先将日期设置为本月的第一天,然后减去一天就变成了上个月的最后一天了:
		// calendar.set(Calendar.DAY_OF_MONTH, 1);
		// calendar.add(Calendar.DATE, -1);

		return lastdaylastmonth;
	}

	/** 判断日期为星期几,0为星期天,依此类推 */
	public static String dayOfWeek(String strdate) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
		Date date = null;
		try {
			date = df.parse(strdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			System.out.println("字符型转日期有异常");
		}
		// 首先定义一个calendar,必须使用getInstance()进行实例化
		Calendar aCalendar = Calendar.getInstance();
		// 里面野可以直接插入date类型
		aCalendar.setTime(date);
		// 计算此日期是一周中的哪一天
		int x = aCalendar.get(Calendar.DAY_OF_WEEK) - 1;
		String week = "";
		switch (x) {
		case 0:
			week = "星期天";
			break;
		case 1:
			week = "星期一";
			break;
		case 2:
			week = "星期二";
			break;
		case 3:
			week = "星期三";
			break;
		case 4:
			week = "星期四";
			break;
		case 5:
			week = "星期五";
			break;
		case 6:
			week = "星期六";
			break;

		}
		return week;
	}

	/** 计算两个日期相隔的天数 */
	public static int nDaysBetweenTwoDate(Date firstDate, Date secondDate) {
		int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
		return nDay;
	}

	/** 计算两个日期相隔的天数 */
	public static int nDaysBetweenTwoDate(String firstString,
			String secondString) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date firstDate = null;
		Date secondDate = null;
		try {
			firstDate = df.parse(firstString);
			secondDate = df.parse(secondString);
		} catch (Exception e) {
			// 日期型字符串格式错误
		}

		int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));

		return nDay;
	}

	/** 某年某月有多少天 */
	public static int yearMonthDay(int year, int month) {
		int day = 0;
		if (((month >= 1) && (month <= 12)) && (year >= 0)) {
			if (month == 2) {
				if ((((year % 4) == 0) && ((year % 100) != 0))
						|| ((year % 400) == 0))
					day = 29;
				else
					day = 28;
			} else {
				if ((month == 1) || (month == 3) || (month == 5)
						|| (month == 7) || (month == 8) || (month == 10)
						|| (month == 12))

					day = 31;
				else
					day = 30;
			}
		}
		return day;
	}

	/** 判断两时间相差的分钟数 */
	public static long minutes(String beginDate, String endDate) {

		Date begin = null;
		Date end = null;
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long minute1 = 0;
		try {
			begin = dfs.parse(beginDate);
			end = dfs.parse(endDate);

			long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
			minute1 = between / 60;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return minute1;

	}

	/** 判断两时间相差的秒数 */
	public static long seconds(String beginDate, String endDate) {

		Date begin = null;
		Date end = null;
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long seconds = 0;
		try {
			begin = dfs.parse(beginDate);
			end = dfs.parse(endDate);

			seconds = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒

		} catch (Exception e) {
			e.printStackTrace();
		}
		return seconds;

	}
	
	/** 判断两时间相差的毫秒数 */
	public static long milliSeconds(String beginDate, String endDate) {
		
		Date begin = null;
		Date end = null;
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long milliSeconds = 0;
		try {
			begin = dfs.parse(beginDate);
			end = dfs.parse(endDate);
			
			milliSeconds = (end.getTime() - begin.getTime());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return milliSeconds;
		
	}

	/** 获取当前时间的季度 */
	public static String getQuarter() {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowDate = dfs.format(new Date());

		String[] str = nowDate.split("-");
		int nowmonth = Integer.parseInt(str[1]);
		if (nowmonth <= 3) {
			return "第一季度";
		} else if (nowmonth > 3 && nowmonth <= 6) {
			return "第二季度";
		} else if (nowmonth > 6 && nowmonth <= 9) {
			return "第三季度";
		} else {
			return "第四季度";
		}
	}

	/** 获取当前第多少周 */
	public static String getWeekDay() {
		TimeZone zone = TimeZone.getTimeZone("Asia/Shanghai");
		Calendar cal = Calendar.getInstance(zone);
		int c = cal.get(cal.WEEK_OF_YEAR);
		return c + "";
	}

	/** 根据日期获取当前多少周 */
	public static String getWeekDayByDate(String input_date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = format.parse(input_date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		return calendar.get(Calendar.WEEK_OF_YEAR) + "";
	}

	// 时区换算为当前北京时间(参数:1.选择的时间,2.时区)
	// public static String hourdiffer(String newStrTimer,int timeZone) throws
	// Exception{
	// int hour=0;//时区相差的时间(小时)
	// int minutes=0;//时区相差的时间(分钟)
	// String marginTimer="";//各时区差
	// Date tmpDate = null;
	// //时间格式转换
	// newStrTimer=newStrTimer.replace("年", "-");
	// newStrTimer=newStrTimer.replace("月", "-");
	// newStrTimer=newStrTimer.replace("日", " ");
	// newStrTimer=newStrTimer.replace("时", ":");
	// newStrTimer=newStrTimer.replace("分", ":");
	// newStrTimer=newStrTimer.replace("秒", "");
	//
	// switch (timeZone) {
	// case 1://北京时区
	// hour=0;
	// minutes=0;
	// break;
	// case 2://美国时区
	// marginTimer="12";
	// hour=Integer.parseInt(marginTimer);
	// minutes=0;
	// //minutes=Integer.parseInt(marginTimer.split(".")[1]);
	// break;
	// case 3://英国时区
	//
	// break;
	// case 4://北京时区
	//
	// break;
	// case 5://北京时区
	//
	// break;
	// case 6://北京时区
	//
	// break;
	// case 7://北京时区
	//
	// break;
	// case 8://北京时区
	//
	// break;
	// case 9://北京时区
	// break;
	// default:
	// break;
	// }
	// tmpDate = sdf2.parse(newStrTimer);
	// Calendar now = Calendar.getInstance();
	// now.setTime(tmpDate);
	// now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hour);
	// now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minutes);
	// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	// return sdf.format(now.getTime());
	// }

	/** 获取本月的第一天 */
	public static String getFirstDayThisMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String first = df.format(c.getTime());
		return first;
	}

	/** 获取本月的最一天 */
	public static String getLastDayThisMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH,
				ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		String last = df.format(ca.getTime());
		return last;
	}

	/** 获取当前时间 */
	public static String getNowDate() {
		String nowdate = "2000-01-01 01:00:00";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			nowdate = df.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nowdate;
	}

	/** 获得当前日期与本周日相差的天数 */
	private static int getMondayPlus(Date gmtCreate) {
		Calendar cd = Calendar.getInstance();
		cd.setTime(gmtCreate);
		// 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {
			return 0;
		} else {
			return 1 - dayOfWeek;
		}
	}

	/** 获得下周星期一的日期 */
	public static String getNextMonday() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		int mondayPlus = getMondayPlus(new Date());
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
		Date monday = currentDate.getTime();
		return df.format(monday);
	}

	/** 获得下周星期天的日期 */
	public static String getNextSunday() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		int mondayPlus = getMondayPlus(new Date());
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 13);
		Date monday = currentDate.getTime();
		return df.format(monday);
	}

	/** 获取下月的第一天 */
	public static String getFirstDayNextMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 1);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String first = df.format(c.getTime());
		return first;
	}

	/** 获取下月的最一天 */
	public static String getLastDayNextMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.MONTH, 1);
		ca.set(Calendar.DAY_OF_MONTH,
				ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		String last = df.format(ca.getTime());
		return last;
	}

	public static void main(String[] args) {
		System.out.println(Integer.parseInt("01"));
	}
}