Android format de date MM-JJ-AAAA de Datepicker
J'ai un datepicker. Mon application est en poussant un avis. Je veux afficher la date dans 01-07-2013 MM-jj-aaaa. Veuillez vérifier mon code ci-dessous:
//---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();
}
}
});
}
AlarmDetails:
@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 );
}
DisplayNotification:
@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();
}
Mon application n'affiche que dans ce format mm-jj-aaaa. J'ai lu que nous devrions utiliser SimpleDateFormat, mais je ne sais pas comment l'utiliser dans mon application. Quelqu'un peut-il aider? Merci.
OriginalL'auteur | 2013-01-07
Vous devez vous connecter pour publier un commentaire.
Vérifier:
modification de format de date dans datepicker et calendrier
OriginalL'auteur Archie.bpgc
essayer cette
c'est beaucoup mieux que la accepté de répondre.
OriginalL'auteur hoi
Vous pouvez ajouter ce code:
après
De sorte que votre strDate sera dans le format que vous souhaitez (MM-jj-aaaa)
Oui, ce sera 'd' à la place de la variable "date". Je suis d'édition
Merci! Je vais essayer cela.
Hey j'ai essayé d'éditer mon code mais il affiche toujours 0-7-2013?
La valeur de date est 3913.
OriginalL'auteur MysticMagicϡ
suffit d'utiliser ce code..
OriginalL'auteur Mehul Ranpara
Une façon simple de le faire pourrait être de simplement recueillir des données, puis utiliser des chaînes de caractères.format:
OriginalL'auteur slogan621
Essayer de cette façon:
OriginalL'auteur GrIsHu
J'ai essayé de suivre dans mon projet. Il pourrait vous aider. Demander si vous avez n'importe quelle requête.
OriginalL'auteur Dhwani
Espère que sa utile.
Vous pouvez également reportez-vous à la documentation.
OriginalL'auteur Ayaz Ali Khatri
oui, vous pouvez le Faire par Étapes ci-Dessous.
vous pouvez l'Utilisateur ci-Dessous le Code pour cela.
Espère que ça va vous aider.
OriginalL'auteur Bhavesh Patadiya
Essayez ceci:
OriginalL'auteur Douglas Mesquita