simpleDateFormat日期格式转换
1-------------------------------------------------------------------------------------
package com.neusoft.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormat01
{
public static void main(String[] args) throws ParseException
{
// TODO Auto-generated method stub
String str = "2009-02-15 09:21:35.345";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒SSS毫秒");
Date date = sdf1.parse(str);//提取格式中的日期
System.out.println("转换之前的日期:"+date);
String newStr = sdf2.format(date); //改变格式
System.out.println("转换之后的日期:"+newStr);
}
}
2-------------------------------------------------------------------------------------
package com.neusoft.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo02
{
public static void main(String []args) throws ParseException{
String str = "2009-02-15 09:21:35.345";//接收參数
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
Date date = sdf.parse(str);//提取格式中的日期
String str1 = sdf1.format(date);
System.out.println(date);
System.out.println(str1);
}
}
3-------------------------------------------------------------------------------------
package com.neusoft.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo03
{
SimpleDateFormat sdf = null;
//2012-03-05
public String DateFormat(){
this.sdf = new SimpleDateFormat("yyyy-MM-dd");
String str = this.sdf.format(new Date());
return str;
}
//2012-03-05 05:27:15:390
public String DateFormat1(){
this.sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
String str = this.sdf.format(new Date());
return str;
}
//2012年03月05日
public String dateNewFormat(){
this.sdf = new SimpleDateFormat("yyyy年MM月dd日");
String str = this.sdf.format(new Date());
return str;
}
//2012年03月05日05时26分19秒437毫秒
public String dateNewFormat1(){
this.sdf = new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒SSS毫秒");
String str = this.sdf.format(new Date());
return str;
}
public String dateNewFormat2(){
this.sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
String str = this.sdf.format(new Date());
return str;
}
public static void main(String []args) throws ParseException{
SimpleDateFormatDemo03 ss= new SimpleDateFormatDemo03();
System.out.println(ss.DateFormat());
System.out.println(ss.DateFormat1());
System.out.println(ss.dateNewFormat());
System.out.println(ss.dateNewFormat1());
System.out.println(ss.dateNewFormat2());
}
}