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(String[] args) throws Exception {
    // Declare a date format for parsing
    SimpleDateFormat dateParser = new SimpleDateFormat("h:mm a");

    // Parse the time string
    Date date = dateParser.parse("3:30 PM");

    // Declare a date format for printing
    SimpleDateFormat dateFormater = new SimpleDateFormat("HH:mm");

    // Print the previously parsed time
    System.out.println(dateFormater.format(date));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyy");
    Date date1 = sdf.parse("Thu Oct 03 07:47:22 2015");
    Date date2 = sdf.parse("Mon Jul 05 08:47:22 2015");

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

    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);/*from w  ww. j  ava2  s.co  m*/
    cal2.setTime(date2);

    if (cal1.after(cal2)) {
        System.out.println("Date1 is after Date2");
    }

    if (cal1.before(cal2)) {
        System.out.println("Date1 is before Date2");
    }

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

From source file:Main.java

public static void main(String[] args) throws ParseException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
    Calendar cal = Calendar.getInstance();
    String today = dateFormat.format(cal.getTime());
    System.out.println(today);//from w  ww  .  ja  v  a2s  . c  o m

    SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
    Date actualdate = sdf.parse(today);
    Date date1 = sdf.parse("20042014");
    Date date2 = sdf.parse("23042015");
    Calendar actual = Calendar.getInstance();
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    actual.setTime(actualdate);
    cal1.setTime(date1);
    cal2.setTime(date2);

    System.out.println(actual);
    System.out.println(cal1);
    System.out.println(cal2);

    if (actualdate.after(date1) && actualdate.before(date2)) {
        System.out.println("Yes");
    } else {
        System.out.println("No");
    }

    if (actual.after(cal1) && actual.before(cal2)) {
        System.out.println("Yes");
    } else {
        System.out.println("No");
    }

}

From source file:Main.java

public static void main(String[] argv) {
    java.util.Date date;/*from  w  w  w.  ja va2s .c  o  m*/
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    // Lenient
    try {
        date = sdf.parse("40/02/2015");
        System.out.println("Lenient date is :                  " + date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    // Rigorous
    sdf.setLenient(false);

    try {
        date = sdf.parse("40/02/2015");
        System.out.println("Rigorous date (won't be printed!): " + date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

From source file:MainClass.java

public static void main(String[] args) throws ParseException {
    try {//ww  w  .j  a v a2s  .  co m
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        String date = "2003/01/10";
        java.util.Date utilDate = formatter.parse(date);
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        System.out.println("date:" + date);
        System.out.println("sqlDate:" + sqlDate);
    } catch (ParseException e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }

}

From source file:Main.java

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

    SimpleDateFormat in = new SimpleDateFormat("MM/dd");
    SimpleDateFormat out = new SimpleDateFormat("MMMM, dd");

    System.out.println(out.format(in.parse("07/08")));

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
    String input = "2020-11-11";
    System.out.print(input + " Parses as ");
    Date t = ft.parse(input);
    System.out.println(t);//from  w  w w. java 2  s .  com
}

From source file:Main.java

public static void main(String[] args) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

    List<Date> otherDates = Arrays.asList(new Date[] { simpleDateFormat.parse("01.01.2015 01:00:00"),
            simpleDateFormat.parse("01.01.2015 01:00:02") });

    System.out.println(get(otherDates, simpleDateFormat.parse("01.01.2015 01:00:01")));
    System.out.println(get(otherDates, simpleDateFormat.parse("01.01.2015 01:00:03")));
    System.out.println(get(otherDates, simpleDateFormat.parse("01.01.2015 01:00:00")));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String source = new String("03/20/2015");
    SimpleDateFormat sdfinput = new SimpleDateFormat("MM/dd/yyyy");
    SimpleDateFormat sdfoutput = new SimpleDateFormat("yyyy-MM-dd");
    Date inputdate = sdfinput.parse(source);
    String outputDate = sdfoutput.format(inputdate);
    System.out.println(outputDate);
}

From source file:MainClass.java

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    GregorianCalendar gc = new GregorianCalendar();
    java.util.Date d = sdf.parse("12/12/2003");
    gc.setTime(d);/*from ww  w  .  j  a va 2 s.c o m*/
    System.out.println("Input Date = " + sdf.format(d));
    int dayBefore = gc.get(Calendar.DAY_OF_YEAR);
    gc.roll(Calendar.DAY_OF_YEAR, -1);
    int dayAfter = gc.get(Calendar.DAY_OF_YEAR);
    if (dayAfter > dayBefore) {
        gc.roll(Calendar.YEAR, -1);
    }
    gc.get(Calendar.DATE);
    java.util.Date yesterday = gc.getTime();
    System.out.println("Yesterdays Date = " + sdf.format(yesterday));

}