java String和Date转换

import java.text.SimpleDateFormat;
import java.util.Date;
public class DateRge{
public static void init(){
SimpleDateFormat  format1 = new SimpleDateFormat("yyyy-MM-dd");                
Date date = null;   
String str = null;                 
           
// StringתDate   
str = "2007-1-18";         
try {   
    date = format1.parse(str);  
} catch (Exception e) {   
    e.printStackTrace();   
}   
//DateתString
date=new Date();
str=format1.format(date);
System.out.println(str);
}
public static void main(String[] arg){
    DateRge d=new DateRge();
	d.init();
}
}

你可能感兴趣的:(java)