NikZim
NikZim

Reputation: 29

Problems with date parsing

Good evening, please tell me how to solve the problem with date parsing. The data is written in txt. file, when I read it and parse ( including the date of the object) throws an error with the date (((please help me fix it. I use only javaCore.

public class DemoRoom {

public static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");

public static void main(String[] args) throws Exception {
    Path path = new Path();
    RoomRepository roomRepository = new RoomRepository();


    Hotel hotel1 = new Hotel(1L, "Ajax", "Holland", "Amsterdam", "Amsterdamskaja");
    Hotel hotel2 = new Hotel(2L, "Slask", "Poland", "Wroclaw", "Wroclawskaja");
    Hotel hotel3 = new Hotel(3L, "Dynamo", "Ukraine", "Kyiv", "Obolonskaja");


    String newDate1 = "07-Jun-2019";
    String newDate2 = "10-Nov-2020";
    String newDate3 = "31-Dec-2020";

    Room room1 = new Room(1L, 2, 200.0, false, false, dateFormat.parse(newDate1), hotel1);
    Room room2 = new Room(2L, 5, 1900.99, true, true, dateFormat.parse(newDate2), hotel2);
    Room room3 = new Room(3L, 1, 499.99, true, false, dateFormat.parse(newDate3), hotel3);

    
    roomRepository.addRoom(room1, path.getRoomDB());

Parsing The date is under the fifth element.

    @Override
public Room pars(String str) throws Exception {
    if (str == null)
        throw new Exception("String does not exist!");
    String[] arr = str.split(",");
    Room room = new Room();
    room.setId(Long.parseLong(arr[0]));
    room.setNumberOfGuests(Integer.parseInt(arr[1]));
    room.setPrice(Double.parseDouble(arr[2]));
    room.setBreakfastIncluded(Boolean.parseBoolean(arr[3]));
    room.setPetsAllowed(Boolean.parseBoolean(arr[4]));
    room.setDateAvailableFrom(DemoRoom.dateFormat.parse(arr[5]));
    new HotelRepository().findHotelById((arr[6]));
    return room;
}

Result enter image description here enter image description here

Upvotes: 1

Views: 215

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339888

java.time

You are using terrible date-time classes that were supplanted years ago by the modern java.time framework. Never use Date, Calendar, SimpleDateFormat, and so on.

LocalDate

For a date-only value without time-of-day and without time zone, use LocalDate class.

Avoid exchanging date-time values textually using localized formats. When exchanging strings, use only standard ISO 8601 formats. For a date-only value, that format is YYYY-MM-DD. The java.time classes use ISO 8601 formats by default, so no need to specify a formatting pattern.

LocalDate localDate = LocalDate.parse( "2019-06-07" ) ;

If you must parse localized strings, let DateTimeFormatter do the work.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu" ) ;

Specify the Locale to use in parsing the name of the month.

Locale locale = Locale.US ;  
f = f.withLocale( locale ) ;

Parse your incoming data.

String input = "07-Jun-2019" ; 
LocalDate localDate = LocalDate.parse( input , f ) ;

Your Room class should carry a member field of type LocalDate.

Room x = new Room ( … , localDate , … ) ;

And parse your inputs well before passing to a constructor. You should always expect faulty inputs.

try{
    LocalDate localDate = LocalDate.parse( input , f ) ;
    …
} catch ( DateTimeParseException e ) {
    … 
}

Upvotes: 2

Eric Lang
Eric Lang

Reputation: 274

When you run the code shown below, you're getting calling a method that returns a new instance of SimpleDateFormat that uses the default constructor instead of the constructor defining the format you require ("dd-MMM-yyyy").

    dateFormat.getInstance().parse(newDate1);

Remove the "getInstance()" part, and you'll be golden.

get instance documentation from simple date fromat

Upvotes: 1

Related Questions