Maison  >  Article  >  Java  >  Programme Java pour imprimer les mois dans différents formats

Programme Java pour imprimer les mois dans différents formats

王林
王林avant
2023-08-28 10:33:061284parcourir

Programme Java pour imprimer les mois dans différents formats

Cet article utilise différentes méthodes pour formater les mois à l'aide de différentes bibliothèques et des instructions d'importation correspondantes en langage Java. Il existe de nombreuses façons d'afficher les mois dans la sortie d'un programme Java. Parfois, les mois sont écrits sous forme de chiffres, parfois sous forme longue ou abrégée. Les noms de mois peuvent également être écrits dans d’autres langues, comme l’espagnol, le français, etc.

Algorithme

  • Étape 1 - Demandez à l'utilisateur de saisir une date.

  • Étape 2 - Déterminez la partie mois de la date.

  • Étape 3 - Spécifiez le format du mois.

  • Étape 4 - Imprimez le mois dans le format spécifié.

Plusieurs méthodes

Nous proposons des solutions en utilisant différentes méthodes.

  • En utilisant java.time.Month

  • En utilisant java.util.Date et tableaux

  • En utilisant String et ses méthodes

  • En utilisant java.text.SimpleDateFormat

  • En utilisant java.util.Calendar

Regardons le programme et sa sortie un par un.

Méthode 1 : Utiliser java.time.Month

Dans cette méthode, le mois est imprimé en spécifiant le numéro du mois en commençant par le chiffre 1. Par exemple -> Month.of(1) donnera JANVIER.

Exemple

import java.time.Month;
public class ShowMonth {
   public static void main(String[] args)
   {
      Month mon;
      for (int mn=1; mn<=12; mn++){
         
         // The first month starts with number 1
         mon = Month.of(mn);
         System.out.println(mon);
      }
   }
}

Sortie

JANUARY
FEBRUARY
MARCH
APRIL
MAY
JUNE
JULY
AUGUST
SEPTEMBER
OCTOBER
NOVEMBER
DECEMBER

Méthode 2 : Utiliser java.util.Date et array

Dans cette méthode, le tableau de chaînes est utilisé pour stocker la forme abrégée du mois, comme janvier, février, etc. Utilisez la méthode de sous-chaîne pour identifier la partie mois de la date et imprimez-la dans le format spécifié.

Exemple

import java.util.Date;
public class ShowMonth11{
   public static void main(String[] args) {

      // Months are stored as arrays
      String[] st_arr={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec"};
      String[] seq_arr={"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth","Tenth","Eleventh","Twelfth"};

      // Fetch the current date
      Date dt = new Date();
      String dtstr= dt.toString();
      
      // printing the months
      for (int mn=0; mn<12; mn++){
         System.out.println("The Number " + seq_arr[mn]+ " month in the year is : " +st_arr[mn]);
      }

      //selecting the month part from the date
      String substr2 = dtstr.substring(4,7);
      System.out.println("\nThe Time now is " + dt);
      System.out.println("\nThe current month is " + substr2);
   }
}

Sortie

The Number First month in the year is : Jan
The Number Second month in the year is : Feb
The Number Third month in the year is : Mar
The Number Fourth month in the year is : Apr
The Number Fifth month in the year is : May
The Number Sixth month in the year is : Jun
The Number Seventh month in the year is : Jul
The Number Eighth month in the year is : Aug
The Number Ninth month in the year is : Sep
The Number Tenth month in the year is : Oct
The Number Eleventh month in the year is : Nov
The Number Twelfth month in the year is : Dec
The Time now is Fri Feb 24 13:19:06 IST 2023
The current month is Feb

Méthode 3 : Utiliser des chaînes et leurs méthodes

Dans cette méthode, la date est saisie sous forme de chaîne. Utilisez ensuite la méthode des sous-chaînes pour identifier et imprimer la partie mois. Les mois peuvent être affichés même sous forme numérique.

Exemple

public class ShowMonth22{
   public static void main(String[] args) {
      String dtstr= "02-24-2023";
      String dtstr1= "24-02-2023";
      
      //getting the month part of the date
      String substr2 = dtstr.substring(0,2);
      System.out.println("\nThe date is " + dtstr);
      System.out.println("\nThe current month is " + substr2);
      
      //getting the month part of the date
      String substr3 = dtstr1.substring(3,5);
      System.out.println("\nThe date is " + dtstr1);
      System.out.println("\nThe current month is " + substr3);
   }
}

Sortie

The date is 02-24-2023
The current month is 02
The date is 24-02-2023
The current month is 02

Méthode 4 : utilisez java.text.SimpleDateFormat

Dans cette méthode, vous pouvez spécifier différents formats comme mode d'affichage des dates. Le format du mois dans la date est imprimé au format MMM (par exemple, février) ou au format MMMM (par exemple, février). Il peut également être utilisé pour afficher les noms de mois dans différents formats régionaux et langues. Pour eq, l'espagnol febrero signifie février.

Exemple

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class ShowMonth33{
   public static void main(String[] args) {
      Date dtt = new Date();
      
      // MMM means date specification such as Feb, Mar etc
      SimpleDateFormat formatted_mmm;
      
      // MMMM means date specification such as February, March etc
      SimpleDateFormat formatted_mmmm;
      SimpleDateFormat formatted_datestyle;
      SimpleDateFormat formatted_datestyle1;
      formatted_mmm = new SimpleDateFormat("MMM");
      System.out.println(formatted_mmm.format(dtt));
      formatted_mmmm = new SimpleDateFormat("MMMM");
      System.out.println(formatted_mmmm.format(dtt));
      
      //Specifying the pattern of showing date time
      formatted_datestyle = new SimpleDateFormat("EEEE dd MMMM yyyy kk:mm:ss");
      System.out.println(formatted_datestyle.format(dtt));
      
      //Specifying the pattern of showing date time
      formatted_datestyle1 = new SimpleDateFormat("dd MMM yyyy kk:mm:ss");
      System.out.println(formatted_datestyle1.format(dtt));
      
      //setting for the Spanish language
      Locale spanishDT = new Locale("es","ES");
      System.out.println("Now using Spanish: ");
      System.out.printf(spanishDT, "month: %tB\n", dtt);
      
      //setting for the French language
      SimpleDateFormat dt_fr = new SimpleDateFormat("dd MMM yyyy kk:mm:ss", new Locale("fr"));
      System.out.println("Now using French: ");
      System.out.println(dt_fr.format(dtt));
   }
}

Sortie

Feb
February
Friday 24 February 2023 19:48:31
24 Feb 2023 19:48:31
Now using Spanish:
month: febrero
Now using French:
24 févr. 2023 19:48:31

Méthode 5 : utilisez java.util.Calendar

Dans cette méthode, utilisez l'objet Calender et utilisez Calendar.MONTH pour trouver le mois. Cet index commence à 0, ajoutez donc 1 à Calendar.MONTH pour imprimer le mois correct dans la date.

Exemple

import java.util.Calendar;
public class ShowMonth44{
   public static void main(String[] args) {
      
      //using Calendar instance
      Calendar cal = Calendar.getInstance();
      
      //getting Time from cal
      System.out.println("The Current Date is: " + cal.getTime());
      
      // Calendar Months start with 0 index therefore 1 is added for the correct result
      System.out.println("Current Calendar Month is : " + (cal.get(Calendar.MONTH) +1 ) );
   }
}

Sortie

The Current Date is: Fri Feb 24 19:12:06 IST 2023
Current Calendar Month is: 2

Conclusion

Dans cet article, nous explorons différents formats de mois en utilisant le langage Java. Différents exemples de code sont également fournis avec le résultat de chaque méthode utilisée.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer