java 中如何将“yyyy-MM-dd” 格式转换为“MM-dd-yyyy”

来源:百度知道 编辑:UC知道 时间:2024/09/25 02:21:02
现在有一 String time = "2008-10-31";
现在我想转换为 "10-31-2008" 这种格式,怎么做?

//转换成yyyy-MM-dd的字符串
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
String date = df.format(new Date());
System.out.println("1:"+date);

//字符串转换成日期
ParsePosition pos = new ParsePosition(0);
java.util.Date datetime = df.parse(date, pos);

//再转换成MM-dd-yyyy的字符串
SimpleDateFormat df2 = new SimpleDateFormat("MM-dd-yyyy");//设置日期格式
String date2 = df2.format(datetime);
System.out.println("2:"+date2);

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String time="2008-11-12";
DateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
DateFormat d2 = new SimpleDateFormat("MM-dd-yyyy");
try {