0

我想在代码中将日期格式从1-1-2012To转换。1 January 2012谁能帮帮我吗?

4

4 回答 4

2

您可以使用SimpleDateFormat.

http://developer.android.com/reference/java/text/SimpleDateFormat.html

http://www.exampledepot.com/egs/java.text/formatdate.html

String pattern="dd/MMMM/yyyy";
于 2012-05-28T13:19:27.013 回答
1

仔细阅读这个类... java中的SimpleDateFormat

           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);
于 2012-05-28T13:23:24.297 回答
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);

运行解决方案。

此外,如果您需要本地化输出日期,SimpleDateFormat 的构造函数支持自定义语言环境。

于 2012-05-28T13:26:45.530 回答
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>
于 2012-05-28T13:45:12.307 回答