Example usage for java.text SimpleDateFormat parse

List of usage examples for java.text SimpleDateFormat parse

Introduction

In this page you can find the example usage for java.text SimpleDateFormat 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(final String[] args) {
    final String date = "2015-10-02T12:23:23";
    final String pattern = "yyyy-MM-dd'T'hh:mm:ss";
    final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    try {//from ww  w.j av  a 2 s  .  c om
        Date d = sdf.parse(date);
        System.out.println(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

From source file:MainClass.java

public static void main(String[] args) {
    String pattern = "MM/dd/yyyy";
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    try {/*from w ww  . j  av a2  s.  c o m*/
        Date date = format.parse("12/31/2006");
        System.out.println(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    // formatting
    System.out.println(format.format(new Date()));
}

From source file:Main.java

public static void main(String[] args) throws java.lang.Exception {
    String[] StartTimes = { "10:00", "7:00" };
    String[] EndTimes = { "12:00", "14:56" };
    for (int i = 0; i < StartTimes.length; i++) {
        if (StartTimes != null && StartTimes.length > 0 && EndTimes != null && EndTimes.length > 0) {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            Date date1 = format.parse(StartTimes[i]);
            Date date2 = format.parse(EndTimes[i]);
            long millis = date2.getTime() - date1.getTime();

            String hourminute = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis)
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)));
            System.out.println(hourminute);
        }//from w w  w .j av a 2 s  .  co m
    }
}

From source file:Main.java

public static void main(String[] args) throws java.text.ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    out.println(isBeforeMonths(-1, sdf.parse("14/12/2015")));
    out.println(isBeforeMonths(1, new Date()));
    out.println(isBeforeMonths(-1, sdf.parse("24/12/2015")));
}

From source file:Main.java

public static void main(String[] args) throws ParseException {
    SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd-'T'HH:mm:ss'Z'");
    Date inDate = inFormat.parse("2015-01-11-T00:00:00Z");

    SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMdd");
    String output = outFormat.format(inDate);

    System.out.println("Date: " + output);

}

From source file:MainClass.java

public static void main(String[] args) throws ParseException {
    int year = 2003;
    int month = 12;
    int day = 12;

    String date = year + "/" + month + "/" + day;
    java.util.Date utilDate = null;

    try {/*from   w ww  .  j  ava2s . c o  m*/
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        utilDate = formatter.parse(date);
        System.out.println("utilDate:" + utilDate);
    } catch (ParseException e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/*w  w w  . jav a2s  . c  om*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2015-12-31");
        Date date2 = sdf.parse("2015-01-31");

        System.out.println(sdf.format(date1));
        System.out.println(sdf.format(date2));

        if (date1.compareTo(date2) > 0) {
            System.out.println("Date1 is after Date2");
        } else if (date1.compareTo(date2) < 0) {
            System.out.println("Date1 is before Date2");
        } else if (date1.compareTo(date2) == 0) {
            System.out.println("Date1 is equal to Date2");
        } else {
            System.out.println("How to get here?");
        }

    } catch (ParseException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    Date workDate = simpleDateFormat1.parse("2011-11-27");

    Calendar workCalendar = Calendar.getInstance();
    workCalendar.setTime(workDate);/* w w  w  .  j a  va 2s .  co m*/
    SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm:ss");
    Calendar time = Calendar.getInstance();
    time.setTime(simpleDateFormat2.parse("06:00:00"));
    workCalendar.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
    workCalendar.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
    workCalendar.set(Calendar.SECOND, time.get(Calendar.SECOND));

    Date newWorkDate = workCalendar.getTime();

    SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    System.out.println(simpleDateFormat3.format(newWorkDate));
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   ww  w.  ja  va2  s . c o  m*/

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2015-12-31");
        Date date2 = sdf.parse("2015-01-31");

        System.out.println(sdf.format(date1));
        System.out.println(sdf.format(date2));

        if (date1.after(date2)) {
            System.out.println("Date1 is after Date2");
        }

        if (date1.before(date2)) {
            System.out.println("Date1 is before Date2");
        }

        if (date1.equals(date2)) {
            System.out.println("Date1 is equal Date2");
        }

    } catch (ParseException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    // Prints a value in 2014
    System.out.println(format.parse("16-16-2013"));
    format.setLenient(false);//from  w  w w  . j  a  va2s  .c om
    // Throws an exception
    System.out.println(format.parse("16-16-2013"));
}