Advertisement
Guest User

Datumformat2

a guest
Dec 7th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package vs222my_assign2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class DatumFormat3 {
  6.  
  7.     public static String CorrectDate="";
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner scan=new Scanner(System.in);
  11.         System.out.print("Skriv ett år: ");
  12.         int yy = scan.nextInt();
  13.         System.out.print("Skriv en månad(nummer): ");
  14.         int mm = scan.nextInt();
  15.         System.out.print("Skriv en dag(nummer): ");
  16.         int dd = scan.nextInt();
  17.         System.out.println("Ange format (b/l/m): ");
  18.         String format = scan.next();
  19.         DateFormat(yy, mm, dd, format);
  20.         System.out.println(CorrectDate);
  21.     }
  22.  
  23.  
  24.     public static String DateFormat(int yy, int mm, int dd, String format){
  25.         String month;
  26.         String day;
  27.         if (mm<10){                 //if the number of the month is less than 10 it wont be a zero in front of it
  28.             month="0"+ mm;          // the zero is important because of date-standards
  29.         }
  30.         else {
  31.             month=Integer.toString(mm);     //want all my numbers to be strings because my method returns a string, not a int
  32.         }
  33.         if (dd<10){
  34.             day="0"+ dd;                //do not need to convert int to string. It's done automatically when added into the "day"-string
  35.         }
  36.         else{
  37.             day=Integer.toString(dd);
  38.         }
  39.         if (format.equals("b")){            //.equals to compare strings
  40.             return CorrectDate=yy+"/"+month+"/"+day;
  41.         }
  42.         else if (format.equals("l")){
  43.             return CorrectDate=day+"/"+month+"/"+yy;
  44.         }
  45.         else if (format.equals("m")){
  46.             return CorrectDate=month+"/"+day+"/"+yy;
  47.         }
  48.         else{
  49.             return CorrectDate;
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement