Compare commits
No commits in common. "marc" and "master" have entirely different histories.
|
|
@ -1,16 +1,13 @@
|
|||
package geburtstag;
|
||||
|
||||
|
||||
|
||||
/* Aufgabe a):
|
||||
|
||||
hier bitte die notwendigen imports ergänzen!
|
||||
|
||||
*/
|
||||
|
||||
import java.time.DateTimeException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Geburtstag {
|
||||
|
||||
|
|
@ -18,16 +15,13 @@ public class Geburtstag {
|
|||
public static void main(String[] args) {
|
||||
|
||||
LocalDateTime myBirthday = null;
|
||||
LocalDateTime jetzt = null;
|
||||
LocalDateTime jetzt=null;
|
||||
|
||||
|
||||
myBirthday = setMyBirthday();
|
||||
if (myBirthday != null) {
|
||||
System.out.println("Mein Geburtsdatum mit Geburtszeit ist:");
|
||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEEE, 'der' dd. MMMM yyyy, HH:mm 'Uhr'");
|
||||
System.out.println(myBirthday.format(dtf));
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
System.out.println("Mein Geburtsdatum mit Geburtszeit ist:");
|
||||
System.out.println(myBirthday);
|
||||
|
||||
|
||||
/* Aufgabe c)
|
||||
|
|
@ -37,37 +31,9 @@ public class Geburtstag {
|
|||
|
||||
*/
|
||||
|
||||
jetzt = LocalDateTime.now();
|
||||
|
||||
System.out.println("Jetzt ist:");
|
||||
System.out.println(jetzt.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)) + " Uhr");
|
||||
System.out.println();
|
||||
|
||||
int tageBisGeburtstag = 0;
|
||||
|
||||
// Wie viele Tage hat dieses Jahr? (Schaltjahr?)
|
||||
int tageImJahr = jetzt.getYear() % 4 == 0 ? 366 : 365;
|
||||
|
||||
if (myBirthday != null) {
|
||||
|
||||
// Wann ist der nächste Geburstag?
|
||||
LocalDateTime meinNaechesterGeburtstag = LocalDateTime.of(jetzt.getYear(), myBirthday.getMonth(),
|
||||
myBirthday.getDayOfMonth(), myBirthday.getHour(), myBirthday.getMinute());
|
||||
|
||||
// falls der Geburtstag schon dieses Jahr war
|
||||
if (meinNaechesterGeburtstag.isBefore(jetzt)) {
|
||||
meinNaechesterGeburtstag = meinNaechesterGeburtstag.plusYears(1);
|
||||
tageBisGeburtstag = (tageImJahr - meinNaechesterGeburtstag.getDayOfYear()) - jetzt.getDayOfYear();
|
||||
}
|
||||
// falls der Geburtstag noch kommt
|
||||
else {
|
||||
tageBisGeburtstag = meinNaechesterGeburtstag.getDayOfYear() - jetzt.getDayOfYear();
|
||||
}
|
||||
// Ausgabe
|
||||
System.out.println("Bis zu meinem Geburtstag sind es noch " + tageBisGeburtstag + " Tage.");
|
||||
System.out.println(meinNaechesterGeburtstag.getYear() > jetzt.getYear()
|
||||
? "Mein Geburtstag war dieses Jahr schon."
|
||||
: "Mein Geburtstag kommt dieses Jahr noch.");
|
||||
}
|
||||
System.out.println(jetzt);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +41,8 @@ public class Geburtstag {
|
|||
/* Die setMyBirthday-Methode */
|
||||
public static LocalDateTime setMyBirthday() {
|
||||
|
||||
Scanner myInput = new Scanner(System.in);
|
||||
Scanner myInput = null;
|
||||
LocalDateTime tempDateTime = null;
|
||||
|
||||
/* Aufgabe b)
|
||||
|
||||
|
|
@ -85,61 +52,13 @@ public class Geburtstag {
|
|||
*/
|
||||
|
||||
|
||||
String eingabeDatum = "";
|
||||
boolean eingabeKorrekt = false;
|
||||
|
||||
while (!eingabeKorrekt) {
|
||||
System.out.println("Bitte geben Sie Ihren Geburtstag ein. (tt.mm.jjjj)");
|
||||
eingabeDatum = myInput.nextLine();
|
||||
if (eingabeDatum.matches("((0?[1-9])|([1-3][0-9])).(0?[0-9]|1[1-2]).[1-2][0-9][0-9][0-9]")) {
|
||||
eingabeKorrekt = true;
|
||||
} else {
|
||||
System.err.println("Bitte beachten Sie das Eingabeformat!");
|
||||
}
|
||||
}
|
||||
|
||||
String[] datum = eingabeDatum.split("[.]");
|
||||
|
||||
eingabeKorrekt = false;
|
||||
String eingabeZeit = "";
|
||||
|
||||
while (!eingabeKorrekt) {
|
||||
System.out.println("Bitte geben Sie Ihre Geburts-Zeit ein. (hh:mm)");
|
||||
eingabeZeit = myInput.nextLine();
|
||||
if (eingabeZeit.matches("((1[0-9])|(2[0-3])|(0?[0-9])):(([1-5][0-9])|(0?[0-9])|60)")) {
|
||||
eingabeKorrekt = true;
|
||||
} else {
|
||||
System.err.println("Bitte beachten Sie das Eingabeformat!");
|
||||
}
|
||||
}
|
||||
|
||||
// Eingabe schließen
|
||||
myInput.close();
|
||||
|
||||
String[] zeit = eingabeZeit.split("[:]");
|
||||
|
||||
int jahr = Integer.parseInt(datum[2]);
|
||||
int monat = Integer.parseInt(datum[1]);
|
||||
int tag = Integer.parseInt(datum[0]);
|
||||
int stunde = Integer.parseInt(zeit[0]);
|
||||
int minute = Integer.parseInt(zeit[1]);
|
||||
|
||||
try {
|
||||
return LocalDateTime.of(jahr, monat, tag, stunde, minute);
|
||||
} catch (DateTimeException dte) {
|
||||
System.err.println("Das Format Ihrer Eingabe ist fehlerhaft.");
|
||||
System.out.println("Ihre Eingabe:");
|
||||
System.out.println(eingabeDatum);
|
||||
System.out.println(eingabeZeit);
|
||||
System.out.println();
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Aufgabe d)
|
||||
|
||||
wenn z.B. der Wert für den Monat mit 13 eingegeben wird,
|
||||
bricht das Programm mit einer Exception ab, wie heisst diese?
|
||||
-> DateTimeException
|
||||
|
||||
|
||||
|
||||
Die Aufgaben e) bis g) sind in verschiedenen Varianten zu programmiere!
|
||||
|
||||
|
|
@ -160,6 +79,8 @@ public class Geburtstag {
|
|||
|
||||
*/
|
||||
|
||||
return tempDateTime;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue