Parsing the Time Using a Custom Format - Java Date Time

Java examples for Date Time:Legacy Date Format

Description

Parsing the Time Using a Custom Format

Demo Code

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    try {// w  w w. java 2s  .  co m
      DateFormat formatter = new SimpleDateFormat("hh.mm.ss a");
      Date date = (Date) formatter.parse("02.36.33 PM");
      System.out.println(date);

      formatter = new SimpleDateFormat("HH:mm:ss Z");
      date = (Date) formatter.parse("22:14:02 -0500");
      System.out.println(date);
    } catch (ParseException e) {
    }
  }
}

Result


Related Tutorials