从 Datepicker 将日期格式化为 MM-DD-YYYY

标签 android datepicker date-format

我有一个日期选择器。我的应用正在推送通知。我想以 01-07-2013 MM-dd-yyyy 格式显示日期。请检查下面的代码:

 //---Button view---
    Button btnOpen = ( Button ) findViewById( R.id.btEventDone );
    btnOpen.setOnClickListener( new View.OnClickListener() {

        public void onClick(View v) {  

            String strT = title.getText().toString();
            String strDes = description.getText().toString();

            // check if user did not input anything
            if( strT.isEmpty() || strDes.isEmpty() ){

                Toast.makeText( getBaseContext(), "You have blank fields!", Toast.LENGTH_SHORT ).show();

            }
            else{

            //---use the AlarmManager to trigger an alarm---
            AlarmManager alarmManager = ( AlarmManager ) getSystemService( ALARM_SERVICE );                 

            //---get current date and time---
            Calendar calendar = Calendar.getInstance();       

            //---sets the time for the alarm to trigger---
            calendar.set( Calendar.YEAR, date.getYear() );
            calendar.set( Calendar.MONTH, date.getMonth() );
            calendar.set( Calendar.DAY_OF_MONTH, date.getDayOfMonth() );                 
            calendar.set( Calendar.HOUR_OF_DAY, time.getCurrentHour() );
            calendar.set( Calendar.MINUTE, time.getCurrentMinute() );
            calendar.set( Calendar.SECOND, 0 );

            //---PendingIntent to launch activity when the alarm triggers---                    
            Intent i = new Intent( CalendarEvent.this, DisplayNotification.class );

            year = calendar.get( Calendar.YEAR );
            month = calendar.get( Calendar.MONTH );
            day = calendar.get( Calendar.DAY_OF_MONTH );

            hour = calendar.get( Calendar.HOUR );
            minute = calendar.get( Calendar.MINUTE );

            Date d = new Date(year, month, day);
            SimpleDateFormat dateFormatter = new SimpleDateFormat(
                            "MM-dd-yyyy");
            String strDate = dateFormatter.format(d);

            String strTitle = title.getText().toString();
            String strDescription = description.getText().toString();
            strDate = String.valueOf( month ) + "-" + String.valueOf( day ) + "-" + String.valueOf( year );

            StringBuilder sb = new StringBuilder();
            if(hour>=12){                      
              sb.append(hour-12).append( ":" ).append(minute).append(" PM");
            }else{
              sb.append(hour).append( ":" ).append(minute).append(" AM");
            }
            String strTime = sb.toString();


            //---assign an ID of 1---
            i.putExtra( "NotifID", notifID ); 
            i.putExtra( "Title", strTitle );
            i.putExtra( "Description", strDescription );
            i.putExtra( "Date", strDate  );
            i.putExtra( "Time", strTime );

            PendingIntent displayIntent = PendingIntent.getActivity(
                getBaseContext(), notifID, i, 0 );               


            //---sets the alarm to trigger---
            alarmManager.set( AlarmManager.RTC_WAKEUP, 
                calendar.getTimeInMillis(), displayIntent );
            finish();
        }

        }
    }); 

}

报警详情:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alarmdetails);  

    //---look up the notification manager service---
    NotificationManager nm = ( NotificationManager ) 
        getSystemService( NOTIFICATION_SERVICE );

    title = ( EditText ) findViewById ( R.id.etDetailTitle );
    description = ( EditText ) findViewById ( R.id.etDetailDescription );
    date = ( EditText ) findViewById ( R.id.etDetailDate );
    time = ( EditText ) findViewById ( R.id.etDetailTime );
    done = ( Button ) findViewById ( R.id.btnAlarmDone );

    done.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });

    //---cancel the notification---
    nm.cancel( getIntent().getExtras().getInt( "NotifID" ) ); 


    String strTitle = getIntent().getExtras().getString( "Title" );
    String strDescription = getIntent().getExtras().getString( "Description" );
    strDate = getIntent().getExtras().getString( "Date" );
    String strTime = getIntent().getExtras().getString( "Time" );
    title.setText( strTitle );
    description.setText( strDescription );
    date.setText( strDate );
    time.setText( strTime );


}

显示通知:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

//---get the notification ID for the notification; 
// passed in by the NotifyActivity---
int notifID = getIntent().getExtras().getInt( "NotifID" );
String strTitle = getIntent().getExtras().getString( "Title" );
String strDescription = getIntent().getExtras().getString( "Description" );
String strDate = getIntent().getExtras().getString( "Date" );
String strTime = getIntent().getExtras().getString( "Time" );

//---PendingIntent to launch activity if the user selects 
// the notification---
Intent i = new Intent( DisplayNotification.this, AlarmDetails.class );
i.putExtra( "NotifID", notifID );  
i.putExtra( "Title", strTitle );
i.putExtra( "Description", strDescription );
i.putExtra( "Date", strDate );
i.putExtra( "Time", strTime );

PendingIntent detailsIntent = 
    PendingIntent.getActivity(this, 0, i, 0);

NotificationManager nm = ( NotificationManager )
    getSystemService( NOTIFICATION_SERVICE );
Notification notif = new Notification(
    R.drawable.ic_launcher, 
    "iHealthFirst: Notification!",
    System.currentTimeMillis() );

CharSequence from = "iHealthFirst - New Notification";
CharSequence message = "This is your alert, click to view";        
notif.setLatestEventInfo(this, from, message, detailsIntent);
notif.flags = Notification.FLAG_INSISTENT;
notif.defaults |= Notification.DEFAULT_SOUND;

//---100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms---
notif.vibrate = new long[] { 100, 250, 100, 500};        
nm.notify( notifID, notif );

//---destroy the activity---
finish();
}

我当前的应用程序仅以 M-d-yyyy 这种格式显示。我读过我们应该使用 SimpleDateFormat,但我不知道如何在我的应用程序中使用它。任何人都可以帮忙吗?谢谢。

最佳答案

试试这个

int year = mDatePicker.getYear();
int month = mDatePicker.getMonth();
int day = mDatePicker.getDayOfMonth();

Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);

SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
String strDate = format.format(calendar.getTime());

关于从 Datepicker 将日期格式化为 MM-DD-YYYY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14190968/

相关文章:

javascript - 如何使用带有 jQ​​uery 的日期选择器设置默认的今天日期?

javascript - jQuery 日期选择器,自定义填充所选日期

angularjs - 如何在AngularJS中格式化日期

c# - 以自定义格式设置 DataGridView 列中的日期格式

php - 使用 php 将 'input type text' 作为日期插入 mysql 数据库

java - 在 Android 1.6 上使用 Google Guava

android - 以编程方式重新启动 Android 应用程序

android - 添加 ActionBarSherlock 库会产生涉及 android.support.v4 的错误

android - 如何在Android中使用蓝牙访问另一部手机的文件系统?

javascript - 搜索自定义日期选择器