Dates

Conversion de nombre stockés en date

 private Date getPreferencesTime(){  
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);  
     Date dtC=null;  
     int nAn=preferences.getInt("an",2020);  
     int nMois=preferences.getInt("mois",10);  
     int nJours=preferences.getInt("jours",20);  
     int nH=preferences.getInt("h",10);  
     int nM=preferences.getInt("m",10);  
     int nS=preferences.getInt("s",10);  
     String dtStart = nAn+"-"+nMois+"-"+nJours+"T"+nH+":"+nM+":"+nS+"Z";  
     Log.d("VarDate=","->"+dtStart);  
     //"2010-10-15T09:27:37Z";  
     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
     try {  
       dtC = format.parse(dtStart);  
     } catch (ParseException | java.text.ParseException e) {  
       e.printStackTrace();  
     }  
     return (dtC);  
   }  

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

Date du jour : 

           SimpleDateFormat sdf = new SimpleDateFormat("EEEE d MMMM",Locale.FRANCE);  
           String sDateJour=sdf.format(new Date());  

Donne mercredi 24 décembre

Année actuelle :

 Calendar calendar = Calendar.getInstance();  
 int year = calendar.get(Calendar.YEAR);  
 String sAnActu=""+year;  

Date en fonction de la langue et la localisation :

      private String FormatLocal(String inputDate) {  
           Date dateTrad = null;  
           SimpleDateFormat formatter;  
           formatter = new SimpleDateFormat("dd-MM-yyyy", Locale.FRANCE);  
           try {  
                try {  
                     dateTrad = (Date) formatter.parse(inputDate);  
                } catch (java.text.ParseException e) {  
                     e.printStackTrace();  
                }  
           } catch (ParseException e1) {  
                e1.printStackTrace();  
           }  
           Locale current = context.getResources().getConfiguration().locale;  
           String sDate = SimpleDateFormat.getDateInstance(SimpleDateFormat.FULL,  
                     current).format(dateTrad);  
           return sDate;  
      }  

Plus simple :

  Date date = new Date();  
     Locale current = ctx.getResources().getConfiguration().locale;  
     String sDate = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT,  //ou .LONG
         current).format(date);  
     String sTime = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT,  
         current).format(date);  

A partir d’un texte jj-mm-aaaa affichage de la date en lettres exemple 12-2-2015 = jeudi 12 février :

          SimpleDateFormat sdf = new SimpleDateFormat("EEEE d MMMM");    
          String str_date=pDay+"-"+pMonth+"-"+pYear;  
          Date date = null ;   
          SimpleDateFormat formatter;   
          formatter = new SimpleDateFormat("dd-MM-yyyy");  
          try {  
                     date = (Date)formatter.parse(str_date);  
                } catch (ParseException e1) {  
                     // TODO Auto-generated catch block  
                     e1.printStackTrace();  
                }   
       String sDateJour=sdf.format(date);  

Calendar.HOUR = Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock. E.g., at 10:04:15.250 PM the HOUR is 10.

Calendar.HOUR_OF_DAY = Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.

Affichage d’une date + ou – nb jours de la date actuelle :

SimpleDateFormat sdf = new SimpleDateFormat("EEEE d MMMM",Locale.FRANCE);
SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd",Locale.FRANCE);
String sDtJour=sdf.format(new Date());
Calendar c = Calendar.getInstance();
try {
	c.setTime(sdformat.parse(sDtJour));
    } catch (ParseException e) {
	e.printStackTrace();
    }
c.add(Calendar.DATE, 17);  //-- Ajout 17 jours à la date actuelle
Date resultdate = new Date(c.getTimeInMillis());
sDtJour = sdf.format(resultdate); //-- Contient la date sous la forme mercredi 24 décembre
SimpleDateFormat jjActu = new SimpleDateFormat("d",Locale.FRANCE);
SimpleDateFormat mmActu = new SimpleDateFormat("M",Locale.FRANCE);
int nbjj=Integer.parseInt(jjActu.format(resultdate)); //-- jour sous forme numérique
int nbmm=Integer.parseInt(mmActu.format(resultdate)); //-- mois sous forme numérique

Fonction permettant de calculer le nombre de jours entre la date du jour et la date désirée :

///-- Calcul de la différence entre la date du jour et jj/mm
	int nbjour(int jj, int mm)
	{   SimpleDateFormat formatter = new SimpleDateFormat("dd MM"); 
		// -- ajout de yyyy pour l'année
		Date DateDeb = null, DateFin = null;
		// -- Date du jour
		DateDeb = new Date();
		try {
			DateFin = formatter.parse(jj+" "+mm); 
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		Calendar with = Calendar.getInstance();
		with.setTime(DateDeb);
		Calendar to = Calendar.getInstance();
		to.setTime(DateFin);
		to.set(Calendar.YEAR, with.get(Calendar.YEAR));
		int withDAY = with.get(Calendar.DAY_OF_YEAR);
		int toDAY = to.get(Calendar.DAY_OF_YEAR);
		int diffDay = toDAY - withDAY;
        //-- Pour Debugg
		Toast.makeText(getApplicationContext(), "Nb de jour=" + diffDay,
				Toast.LENGTH_SHORT).show();
		//-- Retourne le nb de jours en + ou -
		return diffDay;
	}

Conversion de secondes en heures, minutes :

 int nSec = 0;  
     try {  
       nSec = Integer.parseInt(sDuree);  
     } catch (NumberFormatException nfe) {  
       Log.e("Erreur", sDuree + " conversion pb");  
     }  
     int hours = nSec / 3600;  
     int minutes = (nSec % 3600) / 60;  
     int seconds = nSec % 60;  
     String sDurTot;  
     if (hours > 0)  
       sDurTot = String.format("%02dh%02d'%02ds", hours, minutes, seconds);  
     else if (minutes > 0)  
       sDurTot = String.format("%02d'%02ds", minutes, seconds);  
     else if (seconds >0)  
       sDurTot = String.format("%02ds", seconds);  
     else  
       sDurTot="";  

Références :

http://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android

Laisser un commentaire

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.

Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
Archives
Catégories