【发布时间】:2011-08-27 00:35:21
【问题描述】:
嗨,
我接收的日期为 MM/dd/yyyy 格式的 String 数据类型,需要转换为 yyyy-MM-dd 格式的数据 Datatype 以存储在 DB 中。
【问题讨论】:
-
到目前为止你尝试过什么?
-
这是同时发生的吗? stackoverflow.com/questions/6027681/… 5 分钟后。 ;)
标签: java
嗨,
我接收的日期为 MM/dd/yyyy 格式的 String 数据类型,需要转换为 yyyy-MM-dd 格式的数据 Datatype 以存储在 DB 中。
【问题讨论】:
标签: java
您可以使用 SimpleDateFormat:
SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat destinationFormat = new SimpleDateFormat("yyyy-MM-dd");
String result = sourceFormat.format(destinationFormat.parse(input));
【讨论】:
使用格式化程序将日期解析为特定类型
String date="07/25/2011";
SimpleDateFormat dateFormatter=new SimpleDateFormat("yyyy-MM-dd");
try {
Date d=dateFormatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
【讨论】:
你也可以这样做
String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
//Instance of the calender class in the utill package
Calendar cal = Calendar.getInstance();
//A class that was used to get the date time stamp
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
打印出你说的时间
System.out.println(sdf.format(cal.getTime()) );
干杯
【讨论】: