title: Getting AM/PM information from LocalDateTime instance
body: <p>I am taking time input from user as a <code>String</code> in <code>Java</code> and then converting it to <code>LocalDateTime</code> object and saving it in a text file.</p>
  
  <p><strong>Problem</strong></p>
  
  <p><code>hh:mm a</code> is the format in which i am taking input form user. If i enter <code>12:30 PM</code>, it is saved in text file with current date as <code>2019-03-20T12:30</code> without indication of <code>AM</code> or <code>PM</code>. </p>
  
  <p>Consequently, when i read it from text file, i get the date-time information without <code>AM</code> or <code>PM</code>.</p>
  
  <p><strong>Question</strong></p>
  
  <p>Why is <code>AM</code> or <code>PM</code> not saved in the text file and how can i get it from <code>LocalDateTime</code> instance?</p>
  
  <p><strong>Code</strong></p>
  
  <p>Following method takes input from user, converts the user input in to <code>LocalDateTime</code> instance and returns it which is then saved to text file as a <code>String</code></p>
  
  <pre><code>private static LocalDateTime getTimeInput(String question) {        
      System.out.print(question);
      String userInput = scanner.nextLine();
  
      userInput = AppointmentManager.validateTimeString(userInput, question);
  
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd h:m a");
      String todaysDateString = LocalDate.now().toString();
      userInput = todaysDateString + " " + userInput;
  
      return LocalDateTime.parse(userInput, formatter);       
  }
  </code></pre>
  
  <p><code>validateTimeString</code> function is used to verify that user input is in correct format</p>
  
  <p>Following method saves the data to text file</p>
  
  <pre><code>private static final File file = new File("appointments_data.txt");
  
  public static void saveAppointmentInfo(Appointment appointment, boolean appendToFile) {     
      try (FileWriter fw = new FileWriter(file, appendToFile);
          BufferedWriter bfw = new BufferedWriter(fw)) {
  
          String str = AppointmentDataManager.getAppointmentInfoAsString(appointment);
  
          bfw.write(str);
          bfw.newLine();
  
      } catch (IOException ex) {
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p><code>getAppointmentInfoAsString</code> method</p>
  
  <pre><code>private static String getAppointmentInfoAsString(Appointment appointment) {
      StringBuilder sb = new StringBuilder();
  
      sb.append(appointment.getPatientId())
         .append(";")
         .append(appointment.getStartTime())
         .append(";")
         .append(appointment.getEndTime())
         .append(";")
         .append(appointment.getDoctor().getName());
  
      return sb.toString();
  }
  </code></pre>