在JSP中 Date s_BIRTHDAY=new SimpleDateFormat("yyyy-mm-dd").parse(request.getParameter("BIRTHDAY"));

有什么错误?在Tomcat6.0中调试的。在JSP中如何将String型转换成Date型... 有什么错误 ?在Tomcat6.0中调试的。在JSP中如何将String型转换成Date型 展开
 我来答
malk47
2011-03-23
知道答主
回答量:29
采纳率:0%
帮助的人:31.5万
展开全部
这是工作中常用的处理方法 所有日期的基本上都涵盖了

/**
* DateUtil.java
* Description: 日期
* Created By Swiftly
* Created On Sep 20, 2008
* Version: 1.0
*
**/
package com.centech.common.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

public static final DateFormat DATE_FORMAT = new SimpleDateFormat(
"MM/dd/yyyy");
public static final DateFormat FULL_DATE_FORMAT = new SimpleDateFormat(
"EEE, MMM d, yyyyy hh:mm:ss aa z");
public static final DateFormat ISO8601_DATE_FORMAT = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SS z");

private static SimpleDateFormat formatter;

public static String shortDate(Date aDate) {
if (aDate == null)
return "";
formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(aDate);
}

public static String mailDate(Date aDate) {
if (aDate == null)
return "";
formatter = new SimpleDateFormat("yyyyMMddHHmm");
return formatter.format(aDate);
}

public static String longDate(Date aDate) {
if (aDate == null)
return "";
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(aDate);
}

public static String shortDateGB(Date aDate) {
if (aDate == null)
return "";
formatter = new SimpleDateFormat("yyyy'年'MM'月'dd'日'");
return formatter.format(aDate);
}

public static String longDateGB(Date aDate) {
if (aDate == null)
return "";
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(aDate);
}

public static String longDateLog(Date aDate) {
if (aDate == null)
return "";
formatter = new SimpleDateFormat("yyyyMMddHHmmss");
return formatter.format(aDate);
}

public static String formatDate(Date aDate, String formatStr) {
if (aDate == null)
return "";
formatter = new SimpleDateFormat(formatStr);
return formatter.format(aDate);

}

public static String LDAPDate(Date aDate) {
if (aDate == null)
return "";
return formatDate(aDate, "yyyyMMddHHmm'Z'");
}

public static Date getDate(String yyyymmdd) {
if (yyyymmdd == null)
return null;
int year = Integer.parseInt(yyyymmdd
.substring(0, yyyymmdd.indexOf("-")));
int month = Integer.parseInt(yyyymmdd.substring(
yyyymmdd.indexOf("-") + 1, yyyymmdd.lastIndexOf("-")));
int day = Integer.parseInt(yyyymmdd
.substring(yyyymmdd.lastIndexOf("-") + 1));
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
return cal.getTime();

}

public static Date parser(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
return sdf.parse(strDate);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static Date parser24(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return sdf.parse(strDate);
} catch (Exception e) {
return null;
}
}

public static Date getShortDate(String date) {
Date shortDate = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
shortDate = formatter.parse(date);
} catch (Exception e) {
shortDate = null;
}
return shortDate;
}

public static boolean equals(Date date1, Date date2) {
if (date1 == null && date2 == null)
return true;
if (date1 == null && date2 != null)
return false;
if (date1 != null && date2 == null)
return false;
return date1.equals(date2);
}

/**
* @return java.util.Date
* @roseuid 3CECAD76033A
*/
public static Date tomorrow() {
java.util.Calendar calender = java.util.Calendar.getInstance();
calender.roll(Calendar.DAY_OF_YEAR, true);
return calender.getTime();
}

/**
* @param date
* @return java.util.Date
* @roseuid 3CECAD760376
*/
public static Date nextDate(java.util.Date date) {
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(date);
cal.roll(java.util.Calendar.DAY_OF_YEAR, 1);
if (isEndOfYear(date, cal.getTime())) {
cal.roll(Calendar.YEAR, true);
cal.roll(java.util.Calendar.DAY_OF_YEAR, 1);
}
return cal.getTime();
}

/**
* @param curDate
* @param rollUpDate
* @return boolean
* @roseuid 3CECAD76038B
*/
private static boolean isEndOfYear(java.util.Date curDate,
java.util.Date rollUpDate) {
return (curDate.compareTo(rollUpDate) >= 0);
}

/**
* @return java.util.Date
* @roseuid 3CECAD7603A0
*/
public static Date yesterday() {
java.util.Calendar calender = java.util.Calendar.getInstance();
calender.roll(Calendar.DAY_OF_YEAR, false);
return calender.getTime();
}

/**
* @param aDate
* @return String
* @roseuid 3CECAD9D03B9
*/
private static final String getDateFormat(java.util.Date aDate) {
if (aDate == null)
return null;
SimpleDateFormat formatter = new SimpleDateFormat("M/d/yyyy");
return formatter.format(aDate);
}

/**
* @param date
* @return String
* @roseuid 3CECAD9D026F
*/
public static String NVL(java.util.Date date) {
if (date == null)
return "";
else
return getDateFormat(date);
}

/**
* @param baseDate
* @param type
* @param num
* @return
*/
public static Date addDate(Date baseDate, int type, int num) {
Date lastDate = null;
try {
Calendar cale = Calendar.getInstance();
cale.setTime(baseDate);
if (type == 1) {
cale.add(Calendar.YEAR, num);
} else if (type == 2) {
cale.add(Calendar.MONTH, num);
} else if (type == 3) {
cale.add(Calendar.DATE, num);
}
lastDate = cale.getTime();
return lastDate;
} catch (Exception e) {
return null;
}

}

public static Date getDate(String strDate, String formatter) {
SimpleDateFormat sdf = new SimpleDateFormat(formatter);
try {
return sdf.parse(strDate);
} catch (Exception e) {
return null;
}
}

/**
* get system date (yyyy-mm-dd)
*/
public static Date getSysDate() {
return new Date(System.currentTimeMillis());
}

/**
* 根据日期计算属相
*
* @param date
* @return
*/
public static String getAnimalSign(Date date) {
String as = "";

int year = date.getYear();
year = year % 12;
as = Constants.ANIMAL_SIGN_CODE[year];

return as;
}

/**
* 根据日期计算星座
*
* @param date
* @return
*/
public static String getConstellation(Date date) {
String constellation = "";

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);

switch (cal.get(Calendar.MONTH)) {
case 0:
constellation = (day >= 21) ? Constants.CONSTELLATION_CODE[10]
: Constants.CONSTELLATION_CODE[9];
break;
case 1:
constellation = (day >= 20) ? Constants.CONSTELLATION_CODE[11]
: Constants.CONSTELLATION_CODE[10];
break;
case 2:
constellation = (day >= 21) ? Constants.CONSTELLATION_CODE[0]
: Constants.CONSTELLATION_CODE[11];
break;
case 3:
constellation = (day >= 21) ? Constants.CONSTELLATION_CODE[1]
: Constants.CONSTELLATION_CODE[0];
break;
case 4:
constellation = (day >= 22) ? Constants.CONSTELLATION_CODE[2]
: Constants.CONSTELLATION_CODE[1];
break;
case 5:
constellation = (day >= 22) ? Constants.CONSTELLATION_CODE[3]
: Constants.CONSTELLATION_CODE[2];
break;
case 6:
constellation = (day >= 23) ? Constants.CONSTELLATION_CODE[4]
: Constants.CONSTELLATION_CODE[3];
break;
case 7:
constellation = (day >= 24) ? Constants.CONSTELLATION_CODE[5]
: Constants.CONSTELLATION_CODE[4];
break;
case 8:
constellation = (day >= 24) ? Constants.CONSTELLATION_CODE[6]
: Constants.CONSTELLATION_CODE[5];
break;
case 9:
constellation = (day >= 24) ? Constants.CONSTELLATION_CODE[7]
: Constants.CONSTELLATION_CODE[6];
break;
case 10:
constellation = (day >= 23) ? Constants.CONSTELLATION_CODE[8]
: Constants.CONSTELLATION_CODE[7];
break;
case 11:
constellation = (day >= 22) ? Constants.CONSTELLATION_CODE[9]
: Constants.CONSTELLATION_CODE[8];
break;
default:
break;
}

return constellation;
}

public static void main(String[] args) {
//String beg="2010-01-30";
//Date beg_date = DateUtil.getDate(beg, "yyyy-MM-dd");

}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式