From d3e7e97bdef533528d3e94c7d5f34c7b7fb37e51 Mon Sep 17 00:00:00 2001 From: "marc.vachon" <marc.vachon@etu.hesge.ch> Date: Thu, 14 Feb 2019 14:44:16 +0100 Subject: [PATCH] Change date format --- src/Appointment.java | 24 ++---------------------- src/Meeting.java | 33 ++++++++++++++++++--------------- src/MyDate.java | 24 +++++++----------------- src/User.java | 3 ++- 4 files changed, 29 insertions(+), 55 deletions(-) diff --git a/src/Appointment.java b/src/Appointment.java index 339e5a9..167db8f 100644 --- a/src/Appointment.java +++ b/src/Appointment.java @@ -17,10 +17,7 @@ public class Appointment { System.out.println("--------------------------------------------------"); System.out.println("Calcul de la date et heure idéal..."); this.most_popular_date(); - System.out.println("Rendez-vous fixé le: " + - this.f_date.get_day() + "/" + - this.f_date.get_month() + "/" + - this.f_date.get_year()); + System.out.println("Rendez-vous fixé le: " + this.f_date.get_date().toString()); System.out.println("Fini"); } @@ -37,7 +34,7 @@ public class Appointment { for(MyDate date1: this.t_date){ for(MyDate date2: this.t_date){ - if(compare_date(date1, date2)) + if(date1.get_date().equals(date2.get_date())) c++; } // if date's most popular @@ -54,21 +51,4 @@ public class Appointment { for(MyDate d: this.t_date){ } } - - /** - * Compare 2 dates - * @param d1 Date format, first date - * @param d2 Date format, second date - * @return true if dates are the same - */ - private Boolean compare_date(MyDate d1, MyDate d2){ - if(d1.get_day() == d2.get_day()){ - if(d1.get_month() == d2.get_month()){ - if(d1.get_year() == d2.get_year()){ - return true; - } - } - } - return false; - } } diff --git a/src/Meeting.java b/src/Meeting.java index ac9c80a..e9d8218 100644 --- a/src/Meeting.java +++ b/src/Meeting.java @@ -7,13 +7,16 @@ import java.text.SimpleDateFormat; public class Meeting{ private static ArrayList<User> table_User; + private static SimpleDateFormat parseDate = new SimpleDateFormat("dd/MM/yyyy"); - public static void main(String[] args) { + public static void main(String[] args) throws Exception { table_User = new ArrayList<User>(); String date = "14/03/2019"; - System.out.println(date_validity(date)); + System.out.println(date_validity(parseDate.parse(date))); + Date d1 = new Date(2019, 3, 21); + System.out.println(parseDate.format(d1)); set_t_user(); @@ -56,7 +59,7 @@ public class Meeting{ System.out.println("Utilisateur :"); System.out.println("Coordonées x, y: " + user.get_location().get_x() + ", " + user.get_location().get_y()); System.out.println("Date possible :"); - user.get_calendar().forEach((d) -> System.out.println(d.get_day() + "/" + d.get_month() + "/" + d.get_year())); + user.get_calendar().forEach((d) -> System.out.println(parseDate.format(d.get_date()))); System.out.println(); } @@ -67,7 +70,7 @@ public class Meeting{ private static void print_calendar(ArrayList<MyDate> cal){ System.out.println("--------------------------------------------------"); System.out.println("General calendar"); - cal.forEach((d) -> System.out.println(d.get_day() + "/" + d.get_month() + "/" + d.get_year())); + cal.forEach((d) -> System.out.println(parseDate.format(d.get_date()))); System.out.println("Fini"); } @@ -76,7 +79,7 @@ public class Meeting{ * their location, * their calendar */ - private static void set_t_user(){ + private static void set_t_user() throws Exception{ System.out.println("Bonjour, veuillez entrer les informations utilisateur:"); boolean end = false; @@ -106,26 +109,26 @@ public class Meeting{ * Set table of date's user * @return ArrayList of date */ - private static ArrayList<MyDate> set_date(){ + private static ArrayList<MyDate> set_date() throws Exception{ ArrayList<MyDate> t_date = new ArrayList<MyDate>(); boolean end = false; Scanner sc = new Scanner(System.in); while(!end){ - System.out.println("Date possible (jour/mois heure:min heure:min):"); + System.out.println("Date possible (jour/mois/année heure:min heure:min):"); String[] line = sc.nextLine().split(" "); if(line.length == 3){ - String[] date = line[0].split("/"); + //String[] date = line[0].split("/"); + String[] from = line[1].split(":"); String[] to = line[2].split(":"); - System.out.println("Vous avez saisi : " + - date[0] + "/" + date[1] + "/2019 de " + + System.out.println("Vous avez saisi : " + line[0] + " de " + from[0] + ":" + from[1] + " à " + to[0] + ":" + to[1]); - MyDate d = new MyDate(new int[]{Integer.parseInt(date[0]), Integer.parseInt(date[1]), 2019}, + MyDate d = new MyDate(parseDate.parse(line[0]), new int[]{Integer.parseInt(from[0]), Integer.parseInt(from[1])}, new int[]{Integer.parseInt(to[0]), Integer.parseInt(to[1])}); @@ -168,14 +171,14 @@ public class Meeting{ return calendar; } - private static Boolean date_validity(String d1){ + private static Boolean date_validity(Date d1){ Boolean error = false; Date d2 = new Date(); // this object contains the current date value - SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); - Date date = new SimpleDateFormat("dd/MM/yyyy").parse(d1); - return new Date().before(date); + + //Date date = new SimpleDateFormat("dd/MM/yyyy").parse(d1); + return d2.before(d1); } /* diff --git a/src/MyDate.java b/src/MyDate.java index ae7bed4..e0a2bdd 100644 --- a/src/MyDate.java +++ b/src/MyDate.java @@ -1,31 +1,21 @@ +import java.util.Date; + public class MyDate{ - private int day; - private int month; - private int year; + private Date date; private Time from; private Time to; - public MyDate(int[] date, int[] from, int[] to){ - this.day = date[0]; - this.month = date[1]; - this.year = date[2]; + public MyDate(Date date, int[] from, int[] to){ + this.date = date; this.from = new Time(from[0], from[1]); this.to = new Time(to[0], to[1]); } - public int get_day(){ - return this.day; - } - - public int get_month(){ - return this.month; - } - - public int get_year(){ - return this.year; + public Date get_date(){ + return this.date; } public int[] get_t_from(){ diff --git a/src/User.java b/src/User.java index 7ec997e..7b5a82e 100644 --- a/src/User.java +++ b/src/User.java @@ -1,6 +1,7 @@ import java.util.*; import java.net.*; import java.io.*; +import java.util.Date; public class User { @@ -51,7 +52,7 @@ public class User { * @param from int[] * @param to int[] */ - public void add_Date(int[] date, int[] from, int[] to){ + public void add_Date(Date date, int[] from, int[] to){ MyDate d = new MyDate(date, from, to); this.calendar.add(d); } -- GitLab