Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package vs222my_assign2;
- import java.util.Scanner;
- public class DatumFormat3 {
- public static String CorrectDate="";
- public static void main(String[] args) {
- Scanner scan=new Scanner(System.in);
- System.out.print("Skriv ett år: ");
- int yy = scan.nextInt();
- System.out.print("Skriv en månad(nummer): ");
- int mm = scan.nextInt();
- System.out.print("Skriv en dag(nummer): ");
- int dd = scan.nextInt();
- System.out.println("Ange format (b/l/m): ");
- String format = scan.next();
- DateFormat(yy, mm, dd, format);
- System.out.println(CorrectDate);
- }
- public static String DateFormat(int yy, int mm, int dd, String format){
- String month;
- String day;
- if (mm<10){ //if the number of the month is less than 10 it wont be a zero in front of it
- month="0"+ mm; // the zero is important because of date-standards
- }
- else {
- month=Integer.toString(mm); //want all my numbers to be strings because my method returns a string, not a int
- }
- if (dd<10){
- day="0"+ dd; //do not need to convert int to string. It's done automatically when added into the "day"-string
- }
- else{
- day=Integer.toString(dd);
- }
- if (format.equals("b")){ //.equals to compare strings
- return CorrectDate=yy+"/"+month+"/"+day;
- }
- else if (format.equals("l")){
- return CorrectDate=day+"/"+month+"/"+yy;
- }
- else if (format.equals("m")){
- return CorrectDate=month+"/"+day+"/"+yy;
- }
- else{
- return CorrectDate;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement