1.DateHandler.java

package Utils.dateHandler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class DateHandler { /**
* 改变日期为String类型:格式为yyyy-MM-dd
* @param date 日期
* @return String类型的转换结果
*/
public static String dateToString(Date date) {
String sdate = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
if(date != null) {
sdate = formatter.format(date);
}
return sdate;
} /**
* 改变日期为String类型:格式为yyyy-MM-dd HH:mm
* @param date 日期
* @return String类型的转换结果
*/
public static String dateToStringHourMinute(Date date) {
String sdate = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if(sdate != null) {
sdate = formatter.format(date);
}
return sdate;
} /**
* 改变日期为String类型:格式自定义
* @param date 日期
* @param format 格式
* @return String类型的转换结果
*/
public static String dateToString(Date date, String format) {
String sdate = "";
SimpleDateFormat formatter = new SimpleDateFormat(format);
if(sdate != null) {
sdate = formatter.format(date);
}
return sdate;
} /**
* 改变String记录的日期为java.util.Date类型
* @param date String类型日期
* @return Date类型转换结果
* @throws ParseException
*/
public static Date changeStringToDate(String date) throws ParseException {
Date t = null;
if((date != null) && (!date.trim().equals(""))) {
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
t = formatter.parse(date);
} catch(ParseException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
return t;
} /**
* 改变String记录的日期为java.util.Date类型,格式自定义
* @param date String类型日期
* @param format 自定义格式模板
* @return Date类型日期转换结果
* @throws ParseException
*/
public static Date changeStringToDate(String date, String format) throws ParseException {
Date t = null;
if((date != null) && (!date.trim().equals(""))) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(format);
t = formatter.parse(date);
} catch(ParseException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
return t;
}
}

 2.使用:

package Utils.dateHandler;

import java.text.ParseException;
import java.util.Date; import org.junit.Test; public class Test111 { @Test
public void test1() throws ParseException{
System.out.println(DateHandler.dateToString(new Date()));
System.out.println(DateHandler.dateToString(new Date(), "yyyy-MM-dd hh:mm:ss"));
System.out.println(DateHandler.dateToStringHourMinute(new Date()));
System.out.println(DateHandler.changeStringToDate("1900-05-06"));
System.out.println(DateHandler.changeStringToDate("1925-02-20 15:25:30","yyyy-MM-dd hh:mm:ss" ));
}
}

2017-09-16
2017-09-16 11:15:10
2017-09-16 11:15
Sun May 06 00:00:00 CST 1900
Fri Feb 20 15:25:30 CST 1925

也可以在JSP页面中使用该工具类:比如:

JSP中引入该类:

<%@ page import="java.util.*,Utils.DateHandler" %>

文本框中取服务器当前时间:

<input class="form-control" value="<%= DateHandler.dateToString(new Date()) %>"  />

结果:

--------------------------------------------------------------------------

更全的日期工具类:

DateHandler.java
package com.tyust.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; import org.junit.Test; public class DateHandler {
/**
* 把一个日期转换成指定格式的字符串
* @param time
* @return
*/
public static String dateToString(Date time) {
String ctime = "";
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd");
if (time != null) {
ctime = formatter.format(time);
}
return ctime;
} public static String dateToStringHourMinute(Date time) {
String ctime = "";
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (time != null) {
ctime = formatter.format(time);
}
return ctime;
}
// 把String转换成Date
public static Date changeStringToDate(String time) throws ParseException {
Date t = null;
if (time != null && !time.trim().equals("")) {
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
t= formatter.parse(time);
} catch (ParseException e) {
e.printStackTrace();
throw new java.lang.RuntimeException(e);
}
}
return t;
}
// 把String转换成Date如:2010-01-07 13:10:10,yyyy-MM-dd HH:mm:ss
public static Date changeStringToDate(String time,String fomat) throws ParseException {
Date t = null;
if (time != null && !time.trim().equals("")) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(fomat);
t= formatter.parse(time);
} catch (ParseException e) {
e.printStackTrace();
throw new java.lang.RuntimeException(e);
}
}
return t;
}
//把Date转换成String如:2010-01-07 13:10:10
public static String dateToString(Date time,String fomat) {
String ctime = "";
SimpleDateFormat formatter = new SimpleDateFormat(fomat);
if (time != null) {
ctime = formatter.format(time);
}
return ctime;
} /**
* 获得下季度第一天
* @author zxg
* @param month
* @return
*/
public static Date getNextSeasonFirstDay(int month){
int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
int season = 1;
if(month>=1&&month<=3){
season = 2;
}
if(month>=4&&month<=6){
season = 3;
}
if(month>=7&&month<=9){
season = 4;
}
if(month>=10&&month<=12){
season = 1;
}
int start_month = array[season-1][0]; Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式
String years = dateFormat.format(date);
int years_value = Integer.parseInt(years);
if(month>=10&&month<=12){
years_value++;
}
int start_days =1;//years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);
String seasonDateStr = years_value+"-"+start_month+"-"+start_days;
Date seasonDate=new Date();
try {
seasonDate = changeStringToDate(seasonDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return seasonDate; }
/**
* 获得季度的第一天和最后一天
* @author zxg
* @param month
* @param isThisSeason:
* (1)true:本季度第一天和最后一天
* (2)false:上季度第一天和最后一天
* @return
*/
public static String getSeasonFirstEndDay(int month,boolean isThisSeason){
int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
int season = 1;
if(month>=1&&month<=3){
if(isThisSeason){
season = 1;
}else{
season = 4;
}
}
if(month>=4&&month<=6){
if(isThisSeason){
season = 2;
}else{
season = 1;
}
}
if(month>=7&&month<=9){
if(isThisSeason){
season = 3;
}else{
season = 2;
}
}
if(month>=10&&month<=12){
if(isThisSeason){
season = 4;
}else{
season = 3;
}
}
int start_month = array[season-1][0];
int end_month = array[season-1][2]; Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式
String years = dateFormat.format(date);
int years_value = Integer.parseInt(years);
if(month>=1&&month<=3&&!isThisSeason){
years_value--;
}
int start_days =1;//years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);
int end_days = getLastDayOfMonth(years_value,end_month);
String seasonDate = years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days;
return seasonDate; }
/**
* 获得下季度的第一天和最后一天
* @author zxg
* @param month
* @return
*/
public static String getDownSeasonFirstEndDay(int month){
int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
int season = 1;
if(month>=1&&month<=3){
season = 2;
}
if(month>=4&&month<=6){
season = 3;
}
if(month>=7&&month<=9){
season = 4;
}
if(month>=10&&month<=12){
season = 1;
}
int start_month = array[season-1][0];
int end_month = array[season-1][2]; Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式
String years = dateFormat.format(date);
int years_value = Integer.parseInt(years);
if(month>=10&&month<=12){
years_value++;
}
int start_days =1;//years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);
int end_days = getLastDayOfMonth(years_value,end_month);
String seasonDate = years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days;
return seasonDate; }
/**
* 获得季度首月的25号
* (业务需求改变,要求改为季度首月10号)
* @author zxg
* @param month
* @param isThisSeason:
* (1)true:本季度首月的25号 (本季度首月10号)
* (2)false:下季度首月的25号 (下季度首月10号)
* @return
*/
public static Date getSeasonTwentyFive(int month,boolean isThisSeason){
int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
int season = 1;
if(month>=1&&month<=3){
if(isThisSeason){
season = 1;
}else{
season = 2;
}
}
if(month>=4&&month<=6){
if(isThisSeason){
season = 2;
}else{
season = 3;
}
}
if(month>=7&&month<=9){
if(isThisSeason){
season = 3;
}else{
season = 4;
}
}
if(month>=10&&month<=12){
if(isThisSeason){
season = 4;
}else{
season = 1;
}
}
int start_month = array[season-1][0]; Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式
String years = dateFormat.format(date);
int years_value = Integer.parseInt(years);
if(month>=10&&month<=12&&!isThisSeason){
years_value++;
}
int start_days =10;
String seasonDateStr = years_value+"-"+start_month+"-"+start_days;
Date seasonDate=new Date();
try {
seasonDate = changeStringToDate(seasonDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return seasonDate; }
/**
* 获得季度首月的26号
* (业务需求改变,要求改为季度首月11号)
* @author zxg
* @param month
* @param isThisSeason:
* (1)true:本季度首月的26号 (本季度首月11号)
* (2)false:上季度首月的26号 (上季度首月11号)
* @return
*/
public static Date getSeasonTwentySix(int month,boolean isThisSeason){
int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
int season = 1;
if(month>=1&&month<=3){
if(isThisSeason){
season = 1;
}else{
season = 4;
}
}
if(month>=4&&month<=6){
if(isThisSeason){
season = 2;
}else{
season = 1;
}
}
if(month>=7&&month<=9){
if(isThisSeason){
season = 3;
}else{
season = 2;
}
}
if(month>=10&&month<=12){
if(isThisSeason){
season = 4;
}else{
season = 3;
}
}
int start_month = array[season-1][0]; Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式
String years = dateFormat.format(date);
int years_value = Integer.parseInt(years);
if(month>=1&&month<=3&&!isThisSeason){
years_value--;
}
int start_days =11/**/;
String seasonDateStr = years_value+"-"+start_month+"-"+start_days;
Date seasonDate=new Date();
try {
seasonDate = changeStringToDate(seasonDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return seasonDate; }
/**
* 获取某年某月的最后一天
* @author zxg
* @param year 年
* @param month 月
* @return 最后一天
*/
public static int getLastDayOfMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
return 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
if (month == 2) {
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
}
return 0;
} /**
* 计算2个日期中间间隔的时间
*/
public static int getIntervalDays(Date startday,Date endday){
if(startday.after(endday)){
Date cal=startday;
startday=endday;
endday=cal;
}
long sl=startday.getTime();
long el=endday.getTime();
long ei=el-sl;
return (int)(ei/(1000*60*60*24)+1);
} /**
* 是否闰年
* @author zxg
* @param year 年
* @return
*/
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
/**
* 获取当天的最后一秒
* @return
*/
public static Date getDayLastSecond(){
Date targetDate = new Date();
String dateStr = dateToString(targetDate);
try {
targetDate = changeStringToDate(dateStr+" 23:59:59","yyyy-MM-dd HH:mm:ss");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("targetDate:"+targetDate);
return targetDate;
}
/**
* 获取当天的第一秒
* @return
*/
public static Date getDayFirstSecond(){
Date targetDate = new Date();
String dateStr = dateToString(targetDate);
try {
targetDate = changeStringToDate(dateStr+" 00:00:00","yyyy-MM-dd HH:mm:ss");
} catch (ParseException e) {
e.printStackTrace();
}
return targetDate;
}
/**
* date:2012-12-01||2012-12-31
* type:第一天:10 最后一天:20;
* getDayFirstLast(这里用一句话描述这个方法的作用)
* (这里描述这个方法适用条件 – 可选)
* @param type
* @return
*Date
* @throws ParseException
* @exception
* @since 1.0.0
*/
public static Date getDayFirstLast(Date dateParm,String type) throws ParseException{
Date targetDate = new Date();
String dateStr=dateToString(dateParm);
if("10".equals(type)){
targetDate = changeStringToDate(dateStr+" 00:00:00","yyyy-MM-dd HH:mm:ss");
return targetDate;
}else if("20".equals(type)){
targetDate = changeStringToDate(dateStr+" 23:59:59","yyyy-MM-dd HH:mm:ss");
return targetDate;
}else{
return new Date();
}
}
public static Date nextday(){
Date date = new Date();
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, 1);
date = calendar.getTime();
return date;
}
/**
* 根据参数得到指定时间段后的一天
* nextDayByType(这里用一句话描述这个方法的作用)
* (这里描述这个方法适用条件 – 可选)
* @param day
* @return
*Date
* @exception
* @since 1.0.0
*/
public static Date nextDayByType(int day){
Date date = new Date();
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, day);
date = calendar.getTime();
return date;
}
public static Date nextDayByType(Date crruDate, int day){
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(crruDate);
calendar.add(Calendar.DATE, day);
crruDate = calendar.getTime();
return crruDate;
}
/*
* 单元测试
*/
@Test
public void dateExample(){
// System.out.println("获得下季度第一天 :"+getNextSeasonFirstDay(4));
// System.out.println("获得本季度首月的26号:"+getSeasonTwentySix(1,true));
// System.out.println("获得上季度首月的26号:"+getSeasonTwentySix(1,false));
//
// System.out.println("获得本季度的第一天和最后一天:"+getSeasonFirstEndDay(10,true));
// System.out.println("获得上季度的第一天和最后一天:"+getSeasonFirstEndDay(10,false));
}
@Test
public void currMonth(){
String str = "" ;
SimpleDateFormat sdf= new SimpleDateFormat( "yyyy-MM-dd" );
Calendar lastDate = Calendar.getInstance();
int month=lastDate.get(Calendar.MONTH);
str=sdf.format(lastDate.getTime());
// System.out.println("month :"+month);
// String dayStr=getSeasonFirstEndDay(9,true);
// String str[]=dayStr.split(";");
// try {
// System.out.println("str[0] :"+changeStringToDate(str[0]));
// } catch (ParseException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println("str[1] :"+str[1]);
}
@Test
public void dateTest() throws ParseException{
System.out.println("获得下季度第一天 :"+dateToString(nextDayByType(changeStringToDate("2013-01-02 14:30:30", "yyyy-MM-dd HH:mm:ss"),12),"yyyy-MM-dd HH:mm:ss"));
} }

DateHandler日期处理工具(JSP中使用后台工具类)的更多相关文章

  1. jsp中怎么调用java类中的方法

    在jsp页面中先要,引入java类 例如: <%@page import="javabean.DbConn"%><!-- 引入包中的"类" - ...

  2. 在python开发工具PyCharm中搭建QtPy环境(详细)

    在python开发工具PyCharm中搭建QtPy环境(详细) 在Python的开发工具PyCharm中安装QtPy5(版本5):打开“File”——“Settings”——“Project Inte ...

  3. JSP中利用JSTL标签对日期格式化

    数据库:Mysql 开发语言:JAVA 页面类型:JSP 对Mysql中的日期类型格式化,在JSP中,如何办呢,很多人说在JAVA后台去日期格式化,无奈了,于是找到了比较靠谱的答案 需要先引入JSTL ...

  4. 在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法

    在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法   ==========方法1: --------1. 选相应 ...

  5. struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)

    JSON主要创建如下两种数据对象: 由JSON格式字符串创建,转换成JavaScript的Object对象: 由JSON格式字符串创建,转换成JavaScript的List或数组链表对象. 更多关于J ...

  6. java后台json如何传递到jsp中解析

    需求:  系统前端jsp使用的是easyUi的datagrid展示了一些任务信息,任务信息中有个状态信息显示的值是数字, 需要根据后台保存的映射关系,将状态显示为描述信息. 原来的jsp前端显示: 解 ...

  7. jsp中/el表达式中将后台传来的时间戳格式化为年月日时分秒

    sp中/el表达式中将后台传来的时间戳格式化为年月日时分秒1.引入相关标签库 <%@taglib prefix="c" uri="http://java.sun.c ...

  8. js和jsp中怎么去获取后台 model.addAttribute()存入的list<。。。>对象

    java 后台List productionGroupList =getProductionGroupList(); model.addAttribute("productionGroupL ...

  9. struts2中的jsp值传到后台action接收的三种方法

    struts2中的Action接收表单传递过来的参数有3种方法: 如,登陆表单login.jsp: <form action="login" method="pos ...

随机推荐

  1. ES和RDBMS区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12090637.html ES7.0之前,一个Index可以设置多个Type ES7.0开始,Type已 ...

  2. Python 列表(List)Ⅰ

    Python 列表(List) 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型 ...

  3. 25.复杂链表的复制(python)

    题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...

  4. php如何实现大文件断点续传-php文件

    总结一下大文件分片上传和断点续传的问题.因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况.http的网络请求中本身就已经具备了分片上传功能,当传输的文件比较大时,http协议自动会将文件 ...

  5. UML——用例视图

    用例视图中交互功能部分被称为用例.   参与者   作为外部用户与系统发生交互作用,这是参与者的特征. 在系统的实际运作中,一个实际用户可能对应系统的多个参与者.不同的用户也可以只对应于一个参与者,从 ...

  6. Devexpress MVC DropDownList (持续更新))

    @Html.DevExpress().DropDownEdit(settings => { settings.Name = "psBankCharge"; settings. ...

  7. [CF780C]Andryusha and Colored Balloons 题解

    前言 完了,完了,咕值要没了,赶紧写题解QAQ. 题意简述 给相邻的三个节点颜色不能相同的树染色所需的最小颜色数. 题解 这道题目很显然可以用深搜. 考虑题目的限制,如果当前搜索到的点为u, 显然u的 ...

  8. ModelSerializer 使用知识点_序列化和反序列化用法区别

    1.ModelSerializer  如下 from api_test.errorCode.errorCode import Statusclass RelatedbSerializer(serial ...

  9. redis 安装 主从同步 哨兵模式

    一.redis 的安装1.先将安装包放到linux的一个文件夹下面 2.解压压缩包如图所示 3.解压后进入解压文件 4.安装: make 出现it.s a good idea to run 'make ...

  10. linux下vsftpd的安装及配置使用详细步骤(推荐)

    vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点. vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux.BS ...