ToolUtil.java
资源名称:(J2EE)oa.rar [点击查看]
上传用户:lm2018
上传日期:2015-12-12
资源大小:30449k
文件大小:3k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.oa.util;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.TimeZone;
- public class ToolUtil {
- public static String getDate(){
- Date date = new Date();
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- String d =df.format(date);
- return d;
- }
- public static String getNowDate(){
- Date date = new Date();
- String SIMPLE_DATE_FORMAT = "yyyy-MM-dd HH:mm";
- SimpleDateFormat simpleDateFormat;
- simpleDateFormat = new SimpleDateFormat(SIMPLE_DATE_FORMAT);
- simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
- return null==date?null:simpleDateFormat.format(date);
- }
- public static int parseInt(String str){
- int result =0;
- try {
- result = Integer.parseInt(str);
- } catch (Exception e) {
- result =0;
- }
- return result;
- }
- public static String plusTime(String datehm, int mm) {
- //拆分日期和时间,存入长度为2的字符串数组
- String[] dt = datehm.split(" ");
- //拆分年、月、日,存入长度为3的字符串数组
- String[] ymd = dt[0].split("-");
- int year = ToolUtil.parseInt(ymd[0]);
- int month = ToolUtil.parseInt(ymd[1]);
- int day = ToolUtil.parseInt(ymd[2]);
- //拆分小时和分钟,存入长度为2的字符串数组
- String[] hm = dt[1].split(":");
- int maxday = 0;//当月最大天数
- String number = "01,03,05,07,08,10,12";//大月31天
- String[] nums = number.split(",");
- for (int i = 0; i < nums.length; i++) {
- if(ymd[1].equals(nums[i])){
- maxday = 31;
- break;
- }
- }
- String number1 = "04,06,09,11";//小月30天
- String[] nums1 = number1.split(",");
- for (int i = 0; i < nums1.length; i++) {
- if(ymd[1].equals(nums1[i])){
- maxday = 30;
- break;
- }
- }
- if( year%400==0 || (year%4==0 && year%100!=0)){//判断闰年
- if(month==2){
- maxday = 28;
- }
- }else{
- if(month==2){
- maxday = 29;
- }
- }
- int hour = ToolUtil.parseInt(hm[0]);
- int minute = ToolUtil.parseInt(hm[1]);
- minute = minute + mm;
- if(minute>=60){//相加后分钟是否超过最大分钟
- minute = minute - 60;
- hour = hour+1;
- }
- if(hour>=24){//相加后时钟是否超过最大时钟
- hour = hour - 24;
- day = day+1;
- }
- if(day>maxday){//相加后日期是否超过当月最大天数
- day = day - maxday;
- month = month+1;
- }
- if(month>12){//相加后月份是否超过最大月数
- month = month - 12;
- year = year + 1;
- }
- String newdate = year+"-";
- if(month<10){
- newdate+= "0"+month+"-";
- }else{
- newdate+= month+"-";
- }
- if(day<10){
- newdate+= "0"+day;
- }else{
- newdate+= day;
- }
- if(hour<10){
- newdate+= " 0"+hour+":";
- }else{
- newdate+= " "+hour+":";
- }
- if(minute<10){
- newdate+= "0"+minute;
- }else{
- newdate+= minute;
- }
- return newdate;
- }
- }