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. ESP8266--TCP Server

    所谓server,可以简单理解为提供服务,提供数据的一个地方 ESP8266上建立一个server是比较简单的,不过是属于局域网内的server,因为真正意义上的server并不是这样的,大伙了解一个 ...

  2. 百度地图api的简单应用(一):POI检索

    使用之前,需要注册一个百度地图开发者账号,最好申请一个认证以获取更高的使用配额和并发上限. 注册之后,申请一个应用,获得一个ak(密钥),并填写ip地址白名单.(这里我使用0.0.0.0/0,查了自己 ...

  3. 【NOIP2016提高组day1】†换教室

    题目 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的 课程. 在可以选择的课程中,有 2n 节课程安排在 n 个时间段上. 在第 i ( 1 ≤ i ≤ n )个 时间段上,两 ...

  4. 11. ClustrixDB 管理文件空间和数据库容量

    ClustrixDB监视集群中可用的空间量,并主动警告潜在的容量问题.确定集群容量的阈值是可配置的,如下所述. 存储类型 要了解如何管理设备和数据库的利用率,必须首先了解ClustrixDB如何分配磁 ...

  5. 访问SpringBoot中的Swagger的方法

    1.首先启动springboot+swagger的工程. 2.在application.yml里面查看服务的端口号,比如这里是9510. 3.访问URL:http://localhost:9510/t ...

  6. SQLite为何要用C语言来开发?

    SQLite 选择 C 语言的理由是?为什么不选择 Go 或者 Rust? C 语言是最好的 SQLite 在 2000 年 5 月 29 日发布,并一直使用 C 语言实现.C 语言一直是实现 SQL ...

  7. ubuntu下恢复被rm删除的文件

    ubuntu是文件系统,不像windows系统划分盘符(C/D/E/etc...盘).ubuntu对磁盘划分分区,可以使用extundelete恢复ext3/ext4格式的磁盘分区. 其中,ext3/ ...

  8. 搭建一套简单的web服务器,记录实验过程

    搭建web服务器 一.实验内容: 实验要求: 1.完成一个简单的web服务器,web服务器从mysql里读取数据进行返回 2.Mysql需要有一个单独的数据盘,每个mysql虚拟机的磁盘挂载方式需要都 ...

  9. selenium,控制滚动条

    今天写selenium用例的时候,遇见奇葩的问题,FF下是没有错误的,但是在chrome和ie下就会有问题,后来发现是 操作中点击一个按钮,在页面不可见,就会导致异常,解决方法如下: element ...

  10. Elasticsear搭建

    2.1:创建用户: (elasticsearch不能使用root用户) useradd angelpasswd angel 2.2:解压安装包 tar -zxvf elasticsearch-5.5. ...