www.3112.net > jAvA string 转DAtE

jAvA string 转DAtE

ava中String和Date的互相转换使用SimpleDateFormat来完成.SimpleDateFormat使用记得 import java.text.SimpleDateFormat.String -> Date123 java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd "); String s= "

用simpledateformat来转换simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");date date = sdf.parse("2008-08-08 12:10:12");具体请参考jdk api文档另外 转换成date类型后并不会因为转换前的字符串是什么样子而不同

用SimpleDateFormat来转换 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse("2008-08-08 12:10:12"); 具体请参考JDK API文档 另外 转换成DATE类型后并不会因为转换前的字符串是什么

用正则表达式区分,分别处理.

这个方法就可以的.public static date strtodatelong(string strdate) { simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss"); parseposition pos = new parseposition(0); date strtodate = formatter.parse(strdate, pos); return strtodate;} 或者java.sql.date.valueof( "2011-08-01");

public class DateUtil { public static final String format1 = "yyyy-MM-dd"; public static final String format2 = "yyyy-MM-dd HH:mm:ss"; public static final String format3 = "HH:mm:ss"; public static final String format4 = "yyyy-MM-dd HH"; public

转换方法: public static java.util.Date StringToData(String date){ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date tdate = null; try { tdate = formatter.parse(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return tdate; }

多看看源码吧,SimpleDateFormate是可以在String和Date之间转换的

String s = "2012-08-25"; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年M月d日"); try { System.out.println(sdf2.format(sdf1.parse(s))); } catch (ParseException e) { // TODO Auto-generated catch block //www.heatpress123.net e.printStackTrace(); }

试试 simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");

网站地图

All rights reserved Powered by www.3112.net

copyright ©right 2010-2021。
www.3112.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com