-
Java中从String到Date的转换
##Java String和Date的转换 在Java中数据类型的转换是很重要的,这里介绍一 下如何从String 向Date进行转换 ###String—>Date方法一: String dateString = "2012-12-06 "; try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "); Date date = sdf.parse(dateString); } catch (ParseException e) {
System.out.println(e.getMessage()); } ###String—>Date方法二: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; importorg.apache.commons.lang.StringUtils; /** * 日期Util类 * * @author calvin */ public class DateUtil { private static String defaultDatePattern = "yyyy-MM-dd "; /** * 获得默认的 date pattern */ public static String getDatePattern() { return defaultDatePattern; } /** * 返回预设Format的当前日期字符串 */ public static String getToday() { Date today = new Date(); return format(today); } /** * 使用预设Format格式化Date成字符串 */ public static String format(Date date) { return date == null ? " " : format(date, getDatePattern()); } /** * 使用参数Format格式化Date成字符串 */ public static String format(Date date, String pattern) { return date == null ? " " : new SimpleDateFormat(pattern).format(date); } /** * 使用预设格式将字符串转为Date */ public static Date parse(String strDate) throws ParseException { return StringUtils.isBlank(strDate) ? null : parse(strDate, getDatePattern()); } /** * 使用参数Format将字符串转为Date */ public static Date parse(String strDate, String pattern) throws ParseException { return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat( pattern).parse(strDate); } /** * 在日期上增加数个整月 */ public static Date addMonth(Date date, int n) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, n); return cal.getTime(); } public static String getLastDayOfMonth(String year, String month) { Calendar cal = Calendar.getInstance(); // 年 cal.set(Calendar.YEAR, Integer.parseInt(year)); // 月,因为Calendar里的月是从0开始,所以要-1 // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1); // 日,设为一号 cal.set(Calendar.DATE, 1); // 月份加一,得到下个月的一号 cal.add(Calendar.MONTH, 1); // 下一个月减一为本月最后一天 cal.add(Calendar.DATE, -1); return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号 } public static Date getDate(String year, String month, String day) throws ParseException { String result = year + "- " + (month.length() == 1 ? ("0 " + month) : month) + "- " + (day.length() == 1 ? ("0 " + day) : day); return parse(result); } } ###Date—>String 1 String sdate; 2 Date ddate; 3 sdate=(new SimpleDateFormat("yyyy-MM-dd")).format(ddate); SimpleDateFormat函数语法: G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期 D 一年中的第几天 F 一月中第几个星期几 w 一年中第几个星期 W 一月中第几个星期 a 上午 / 下午 标记符 k 时 在一天中 (1~24) K 时 在上午或下午 (0~11) z 时区 常见标准的写法"yyyy-MM-dd HH:mm:ss",注意大小写,时间是24小时制,24小时制转换成12小时制只需将HH改成hh,不需要另外的函数。 ###js格式化时间调用代码 Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+" : this.getDate(), //日 "h+" : this.getHours(), //小时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "q+" : Math.floor((this.getMonth()+3)/3), //季度 "S" : this.getMilliseconds() //毫秒 }; if(/(y+)/.test(fmt)) { fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o) { if(new RegExp("("+ k +")").test(fmt)){ fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); } } return fmt; } var v3=window.Engine.FormItems['enddate'].getValue().format("yyyy-MM-dd"); new date.format(格式)
System.out.println(e.getMessage()); } ###String—>Date方法二: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; importorg.apache.commons.lang.StringUtils; /** * 日期Util类 * * @author calvin */ public class DateUtil { private static String defaultDatePattern = "yyyy-MM-dd "; /** * 获得默认的 date pattern */ public static String getDatePattern() { return defaultDatePattern; } /** * 返回预设Format的当前日期字符串 */ public static String getToday() { Date today = new Date(); return format(today); } /** * 使用预设Format格式化Date成字符串 */ public static String format(Date date) { return date == null ? " " : format(date, getDatePattern()); } /** * 使用参数Format格式化Date成字符串 */ public static String format(Date date, String pattern) { return date == null ? " " : new SimpleDateFormat(pattern).format(date); } /** * 使用预设格式将字符串转为Date */ public static Date parse(String strDate) throws ParseException { return StringUtils.isBlank(strDate) ? null : parse(strDate, getDatePattern()); } /** * 使用参数Format将字符串转为Date */ public static Date parse(String strDate, String pattern) throws ParseException { return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat( pattern).parse(strDate); } /** * 在日期上增加数个整月 */ public static Date addMonth(Date date, int n) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, n); return cal.getTime(); } public static String getLastDayOfMonth(String year, String month) { Calendar cal = Calendar.getInstance(); // 年 cal.set(Calendar.YEAR, Integer.parseInt(year)); // 月,因为Calendar里的月是从0开始,所以要-1 // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1); // 日,设为一号 cal.set(Calendar.DATE, 1); // 月份加一,得到下个月的一号 cal.add(Calendar.MONTH, 1); // 下一个月减一为本月最后一天 cal.add(Calendar.DATE, -1); return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号 } public static Date getDate(String year, String month, String day) throws ParseException { String result = year + "- " + (month.length() == 1 ? ("0 " + month) : month) + "- " + (day.length() == 1 ? ("0 " + day) : day); return parse(result); } } ###Date—>String 1 String sdate; 2 Date ddate; 3 sdate=(new SimpleDateFormat("yyyy-MM-dd")).format(ddate); SimpleDateFormat函数语法: G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期 D 一年中的第几天 F 一月中第几个星期几 w 一年中第几个星期 W 一月中第几个星期 a 上午 / 下午 标记符 k 时 在一天中 (1~24) K 时 在上午或下午 (0~11) z 时区 常见标准的写法"yyyy-MM-dd HH:mm:ss",注意大小写,时间是24小时制,24小时制转换成12小时制只需将HH改成hh,不需要另外的函数。 ###js格式化时间调用代码 Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+" : this.getDate(), //日 "h+" : this.getHours(), //小时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "q+" : Math.floor((this.getMonth()+3)/3), //季度 "S" : this.getMilliseconds() //毫秒 }; if(/(y+)/.test(fmt)) { fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o) { if(new RegExp("("+ k +")").test(fmt)){ fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); } } return fmt; } var v3=window.Engine.FormItems['enddate'].getValue().format("yyyy-MM-dd"); new date.format(格式)
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比