Hej,
w kursie który robię mam string jako dateOfBirth = "10/03/1990"
A nie jest to w formacie daty, którą mógłbym posortować, wyfiltrować, zrobić jakieś between w filtrze itd.
Jak zrobić datę w javie?
Pozdrowienia,
Jacek
Ok to zmieniłem typ na date i teraz problem bo:
Main class:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Person[] people = new Person[] {
new Person("Cleopatra", "Egypt", "69 BC", 1),
new Person("Alexander the Great", "Macedon", "356 BC", 2),
new Person("Julius Caesar", "Rome", "100 BC", 3),
new Person("Hannibal", "Carthage", "247 BC", 4),
new Person("Confucius", "China", "551 BC", 5),
new Person("Pericles", "Greece", "429 BC", 6),
new Person("Spartacus", "Thrace", "111 BC", 7),
new Person("Marcus Aurelius", "Rome", "121 AD", 8),
new Person("Leonidas", "Greece", "540 BC", 9),
new Person("Sun Tzu", "China", "544 BC", 10),
new Person("Hammurabi", "Babylon", "1750 BC", 11),
};
Airline airline = new Airline(people);
for (int i = 0; i < people.length; i++) {
if (people[i].applyPassport() == true) {
people[i].setPassport();
airline.createReservation(people[i]);
}
}
}
}
Person class:
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
public class Person {
private String name;
private String nationality;
private Date dateOfBirth;
String[] passport;
private int seatNumber;
public Person(String name, String nationality, Date dateOfBirth, int seatNumber){
this.name = name;
this.nationality = nationality;
this.dateOfBirth = dateOfBirth;
this.seatNumber = seatNumber;
this.passport = new String[3];
}
public Person(Person source) {
this.name = source.name;
this.nationality = source.nationality;
this.dateOfBirth = source.dateOfBirth;
this.seatNumber = source.seatNumber;
this.passport = Arrays.copyOf(source.passport, source.passport.length);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {this.dateOfBirth = dateOfBirth;}
public String[] getPassport() {
return passport;
}
public void setPassport() {
this.passport = new String[] {this.name, this.nationality, String.valueOf(this.dateOfBirth)
};
}
public int getSeatNumber() {
return seatNumber;
}
public void setSeatNumber(int seatNumber) {
this.seatNumber = seatNumber;
}
public boolean applyPassport () {
Random rd = new Random(); // creating Random object
System.out.println(rd.nextBoolean());
if (rd.nextBoolean() == false) {
return false;
}
else{
return true;
}
}
public void chooseSeat () {
Random rd = new Random(); // creating Random object
this.seatNumber = rd.nextInt(10) + 1;
}
public String toString() {
return "Name: " + this.name + "\n"
+ "Nationality: " + this.nationality + "\n"
+ "Date of Birth: " + this.dateOfBirth + "\n"
+ "Seat Number: " + this.seatNumber + "\n"
+ "Passport: " + Arrays.toString(this.passport) + "\n";
}
}
Jak przypisać to w łatwy sposób? Najlepiej by było po prostu wpisać to jako string ale patrząc przyszłościowo nie chcę mieć textu jako daty.
Co sobie wyobrażam to stworzenie jakiejś klasy która to przerobi, może jakiejś metody?
(próbuję zaczać myśleć obiektowo :) )
Podpowiedźcie proszę,
Jacek
java.time
!? :D
jaryszek napisał(a):
Hej,
w kursie który robię mam string jako dateOfBirth = "10/03/1990"
Rzuć ten kurs w cholerę.
ZrobieDobrze napisał(a):
_13th_Dragon napisał(a):
`Date date =
To jest 20 lat za Murzynami.
Od 10 lat jest java.time
dziekuje.
to java.time czy yoda.time?
Za używanie java.util.Date
jest jest przewidziana kara administracyjna w postaci zainstalowania windows millenium.
Rozwiązanie z obecnej epoki:
https://mkyong.com/java8/java-8-how-to-convert-string-to-localdate/
depricated
... nikt nawet palcem nie kiwni aby to zmienić.
Zarejestruj się i dołącz do największej społeczności programistów w Polsce.
Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.