Date birthday = request.getParameter("birthday");怎么把字符串转换成date类型
答案:2 悬赏:80 手机版
解决时间 2021-03-20 02:27
- 提问者网友:焚苦与心
- 2021-03-19 06:51
Date birthday = request.getParameter("birthday");怎么把字符串转换成date类型
最佳答案
- 五星知识达人网友:舍身薄凉客
- 2021-03-19 07:25
先用String接受,然后调用DateFormat的方法parse返回Date类型!
全部回答
- 1楼网友:零点过十分
- 2021-03-19 07:51
这是工作中常用的处理方法 所有日期的基本上都涵盖了 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); } public static date tomorrow() { java.util.calendar calender = java.util.calendar.getinstance(); calender.roll(calendar.day_of_year, true); return calender.gettime(); } 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(); } private static boolean isendofyear(java.util.date curdate, java.util.date rollupdate) { return (curdate.compareto(rollupdate) >= 0); } public static date yesterday() { java.util.calendar calender = java.util.calendar.getinstance(); calender.roll(calendar.day_of_year, false); return calender.gettime(); } 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); } public static string nvl(java.util.date date) { if (date == null) return ""; else return getdateformat(date); } 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; } } public static date getsysdate() { return new date(system.currenttimemillis()); } public static string getanimalsign(date date) { string as = ""; int year = date.getyear(); year = year % 12; as = constants.animal_sign_code[year]; return as; } 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"); } }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯