Les opérations autour de currentTimeMillis :
long currentTime = System.currentTimeMillis(); Log.d("AndroAuto_Mem_Heure ", currentTime + ""); //now add half an hour, 1 800 000 miliseconds = 30 minutes long halfAnHourLater = currentTime + (30 * 60 * 1000); // 30 * 60 * 1000 //hh*mm*60*1000 Log.d("Heure +30' ", halfAnHourLater + "");
Pour récupérer l’heure et la date du jour :
/** Get the current date and time */ int nMin, nHeure, nDay, nMois, nAn; Calendar cal = new GregorianCalendar(); nAn = cal.get(Calendar.YEAR); nMois = cal.get(Calendar.MONTH)+1; nDay = cal.get(Calendar.DAY_OF_MONTH); nHeure = cal.get(Calendar.HOUR_OF_DAY); //HOUR_OF_DAY nMin = cal.get(Calendar.MINUTE); //HOUR_OF_DAY Log.d("Calendrier ",nHeure+":"+nMin+" "+nDay+"/"+nMois+"/"+nAn);
Et pour le mois :
As is clear by the many answers: the month starts with 0.
Here’s a tip: you should be using SimpleDateFormat to get the String-representation of the month:
Calendar rightNow = Calendar.getInstance();
java.text.SimpleDateFormat df1 = new java.text.SimpleDateFormat("MM");
java.text.SimpleDateFormat df2 = new java.text.SimpleDateFormat("MMM");
java.text.SimpleDateFormat df3 = new java.text.SimpleDateFormat("MMMM");
System.out.println(df1.format(rightNow.getTime()));
System.out.println(df2.format(rightNow.getTime()));
System.out.println(df3.format(rightNow.getTime()));
Output:
11
Nov
November
Note: the output may vary, it is Locale-specific.
Avec API>26 :
LocalDate.now() // Returns a date-only `LocalDate` object for the current month of the JVM’s current default time zone.
.getMonthValue() // Returns 1-12 for January-December.
….