Example usage for java.text DateFormat parse

List of usage examples for java.text DateFormat parse

Introduction

In this page you can find the example usage for java.text DateFormat parse.

Prototype

public Date parse(String source) throws ParseException 

Source Link

Document

Parses text from the beginning of the given string to produce a date.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

    Date today = df.parse("20/12/2005");
    System.out.println("Today = " + df.format(today));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String time = "15:30:18";

    DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    Date date = sdf.parse(time);

    System.out.println("Date and Time: " + date);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DateFormat formatter = new SimpleDateFormat("hh.mm.ss a");
    Date date = (Date) formatter.parse("02.47.44 PM");
    System.out.println(date);/*from   w ww  .  j  a va  2s.  c o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String today = "21/12/2007";

    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date date = formatter.parse(today);
    long dateInLong = date.getTime();

    System.out.println("date = " + date);
    System.out.println("dateInLong = " + dateInLong);
}

From source file:MainClass.java

public static void main(String[] a) throws Exception {

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    Date d1 = df.parse("2001-01-01");

    Date d2 = df.parse("2000-01-01");

    String relation;//from www.  java 2s.co m
    if (d1.equals(d2))
        relation = "the same date as";
    else if (d1.before(d2))
        relation = "before";
    else if (d1.after(d2))
        relation = "after";
    System.out.println(d1 + " is " + relation + ' ' + d2);
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   www. jav  a2 s  .c  o m*/
        String str_date = "11-June-07";
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = (Date) formatter.parse(str_date);
        System.out.println(date);
    } catch (ParseException e) {
        System.out.println("Exception :" + e);
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   ww w . ja v a 2s  . c o m*/
        String target = "Thu Sep 28 20:29:30 JST 2015";
        DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
        Date result = df.parse(target);
        System.out.println(result);
    } catch (ParseException pe) {
        pe.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] a) {
    Date aDate;/*from   w  w w  . j a  v  a 2s  .  co  m*/
    DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
    try {
        aDate = fmt.parse("Saturday, July 4, 1998 ");
        System.out.println("The Date string is: " + fmt.format(aDate));
    } catch (java.text.ParseException e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String fromDateString = "Wed Jul 08 17:08:48 GMT 2015";
    DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    Date fromDate = (Date) formatter.parse(fromDateString);
    TimeZone central = TimeZone.getTimeZone("America/Chicago");
    formatter.setTimeZone(central);// ww w. jav a  2 s. c  om
    System.out.println(formatter.format(fromDate));
}

From source file:CompareDates.java

public static void main(String[] args) throws ParseException {

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    // Get Date 1
    Date d1 = df.parse("2000-02-01");

    // Get Date 2
    Date d2 = df.parse("2001-03-02");

    String relation;//from  ww  w  .j a  va 2 s .c om
    if (d1.equals(d2))
        relation = "the same date as";
    else if (d1.before(d2))
        relation = "before";
    else
        relation = "after";
    System.out.println(d1 + " is " + relation + ' ' + d2);
}