在java中从2012年1月1日到2012年1月1日转换日期格式?

时间:2012-05-28 13:17:05

标签: java android date

我想在代码中将日期格式从1-1-2012转换为1 January 2012。 有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

仔细阅读本课程...... SimpleDateFormat in java

           String DateTime="1-1-2012";

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "dd-MM-yyyy");
        Date myDate = null;
        try {
            myDate = dateFormat.parse(DateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat timeFormat = new SimpleDateFormat("d MMMMM yyyy");
        String finalDate = timeFormat.format(myDate);

        System.out.println(finalDate);

答案 2 :(得分:1)

首先将输入字符串解析为日期,然后将其格式化为首选格式:

String inputDateString = "15-1-2012";
DateFormat dfFrom = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dfFrom.parse(inputDateString);

DateFormat dfTo = new SimpleDateFormat("d MMMM yyyy");
String outputDate = dfTo.format(inputDate);

Run solution.

此外,如果您需要本地化输出日期,SimpleDateFormat's constructor支持自定义区域设置。

答案 3 :(得分:0)

尝试这个工作

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 

        setCurrentDateOnView();
        addListenerOnButton();
    } 
    // display current date
    public void setCurrentDateOnView() {

        tvDisplayDate = (TextView) findViewById(R.id.tvDate);
        dpResult = (DatePicker) findViewById(R.id.datepicker);                  

        final   Calendar c = Calendar.getInstance();                
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);           
        day = c.get(Calendar.DAY_OF_MONTH);             

    //  set current date into textview
        tvDisplayDate.setText(new StringBuilder()
    //  Month is 0 based, just add 1
        .append(month+1).append("-").append(day).append("-")
        .append(year).append(" "));

        tvDisplayDate = (TextView) findViewById(R.id.tvDate);   
        // set current date into datepicker
        dpResult.init(year, month, day, null);
    }       
    public void addListenerOnButton() {         
        btnChangeDate = (Button) findViewById(R.id.btnChangeDate); 
        btnChangeDate.setOnClickListener(new OnClickListener() {     
            public void onClick(View v) { 
                showDialog(DATE_DIALOG_ID);
            }
        });
    }       
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            return new DatePickerDialog(this, datePickerListener,year, month,day);
        }
        return null;
    } 
    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {           

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {   

            Calendar c = Calendar.getInstance();                    
            c.set(selectedYear, selectedMonth, selectedDay);
            year = selectedYear;
            month = selectedMonth;               
            day = selectedDay;      

            string=c.getDisplayName(c.MONTH, LONG, Locale.US);  


            tvDisplayDate.setText(new StringBuilder()
               //     Month is 0 based, just add 1
                    .append(string).append("-").append(day).append("-")
                    .append(year).append(" "));                         

            // set selected date into datepicker also
            dpResult.init(year, month, day, null);
        }
    };  
}    

Xml文件

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > 
<Button
    android:id="@+id/btnChangeDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change " /> 
<TextView
    android:id="@+id/lblDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Current Date (MM-DD-YYYY): "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<DigitalClock
    android:id="@+id/digitalClock1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge"/>

<TextView
    android:id="@+id/tvDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />     

<DatePicker
    android:id="@+id/datepicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />        

</LinearLayout>