Date与String 类型间的转换 yyyy-MMM-dd

来源:互联网 发布:php restful 框架 编辑:程序博客网 时间:2024/06/13 09:46
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/* * date 转换方式 * string->date * 24-四月-2013-> * 24-04-2012-> * date->string  * date->2013-四月-24 * date->2013-04-24 *  * Month: 如果模式字母的数量为 3 或大于 3,则将月份解释为 text;否则解释为 number。 (API原话) *  * */public class DateDemo {public static void main(String[] ager){SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy");SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MMM-dd");String s1 = "24-04-2012";String s2 = "24-四月-2013";try {System.out.println(sdf1.parse(s1));System.out.println(sdf2.parse(s2));Date currDate = new Date();System.out.println(sdf3.format(currDate));System.out.println(sdf4.format(currDate));//输入当前时间System.out.println(currDate);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

运行结果:


原创粉丝点击