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

@Override
public Date parse(String text, ParsePosition pos) 

Source Link

Document

Parses text from a string to produce a Date.

Usage

From source file:Main.java

public static void main(String[] args) {
    String input = "Sun Nov 10 10:00:00 CET 2013";
    SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM d HH:mm:ss ZZZ yyyy");
    System.out.println(parserSDF.parse(input, new ParsePosition(0)));
}

From source file:Main.java

public static void main(String[] args) {
    String input = "2014-05-04 09:10:40.321";

    // Prepare the pattern
    String pattern = "yyyy-MM-dd HH:mm:ss.SSS";

    SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    // Parse the text into a Date object
    Date dt = sdf.parse(input, new ParsePosition(0));
    System.out.println(dt);//from   w w  w .  j  a  v  a2 s .c om

    // Get the Calendar instance
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);

    // Print time parts
    System.out.println("Hour:" + cal.get(Calendar.HOUR));
    System.out.println("Minute:" + cal.get(Calendar.MINUTE));
    System.out.println("Second:" + cal.get(Calendar.SECOND));
    System.out.println("Millisecond:" + cal.get(Calendar.MILLISECOND));

}

From source file:Main.java

public static void main(String[] args) {
    String text = "09/19/2014";
    // Create a pattern for the date text "09/19/2014"
    String pattern = "MM/dd/yyyy";
    SimpleDateFormat simpleFormatter = new SimpleDateFormat(pattern);
    // a ParsePosition object with value zero
    ParsePosition startPos = new ParsePosition(0);
    // Parse the text
    Date parsedDate = simpleFormatter.parse(text, startPos);

    System.out.println(parsedDate);
}

From source file:Main.java

public static void main(String[] args) {
    String text = "ab01/01/1999cd12/31/2000ef";
    String pattern = "MM/dd/yyyy";

    SimpleDateFormat simpleFormatter = new SimpleDateFormat(pattern);

    // Set  the   start index   at 2
    ParsePosition startPos = new ParsePosition(2);

    // Parse the   text to get   the   first date (January 1,  1999) 
    Date firstDate = simpleFormatter.parse(text, startPos);
    System.out.println(firstDate);

    //Now, startPos has  its index set after the last  character of the first date parsed.

    int currentIndex = startPos.getIndex();
    System.out.println(currentIndex);
    // To set its index   to the   next   date increment its index   by  2. 

    int nextIndex = currentIndex + 2;
    startPos.setIndex(nextIndex);//w  w  w  .ja  v a  2  s  . co  m

    // Parse the   text to get   the   second  date (December  31,   2000) 
    Date secondDate = simpleFormatter.parse(text, startPos);
    System.out.println(secondDate);

}

From source file:MainClass.java

public static void main(String[] a) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String input[] = { "2013-10-01 Vancouver, B.C.", "1248-03-01 Ottawa, ON", "1323-06-06 Toronto, ON" };
    for (int i = 0; i < input.length; i++) {
        ParsePosition pp = new ParsePosition(0);
        Date d = formatter.parse(input[i], pp);
        if (d == null) {
            System.err.println("Invalid date in " + input[i]);
            continue;
        }//from   w ww  .ja v a  2s  .  c o  m
        String location = input[i].substring(pp.getIndex());
        System.out.println(" on " + d + " in " + location);

    }
}

From source file:DateParse2.java

public static void main(String[] args) {

    //+/*from  w  ww.  ja v a 2s .  com*/
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String input[] = { "BD: 1913-10-01 Vancouver, B.C.", "MD: 1948-03-01 Ottawa, ON",
            "DD: 1983-06-06 Toronto, ON" };
    for (int i = 0; i < input.length; i++) {
        String aLine = input[i];
        String action;
        switch (aLine.charAt(0)) {
        case 'B':
            action = "Born";
            break;
        case 'M':
            action = "Married";
            break;
        case 'D':
            action = "Died";
            break;
        // others...
        default:
            System.err.println("Invalid code in " + aLine);
            continue;
        }
        int p = aLine.indexOf(' ');
        ParsePosition pp = new ParsePosition(p);
        Date d = formatter.parse(aLine, pp);
        if (d == null) {
            System.err.println("Invalid date in " + aLine);
            continue;
        }
        String location = aLine.substring(pp.getIndex());
        System.out.println(action + " on " + d + " in " + location);
    }
    //-
}

From source file:Main.java

public static Date stringToDate(String dateString) {
    ParsePosition position = new ParsePosition(0);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date dateValue = simpleDateFormat.parse(dateString, position);
    return dateValue;
}

From source file:Main.java

/**
 * Convert String date to Date type/* w  ww . ja va 2  s.c  om*/
 * 
 * @param date
 *            String date
 * @param format
 *            date format
 * @return Date
 */
public static Date stringToDate(String date, String format) {
    if (date == null)
        return null;
    ParsePosition pos = new ParsePosition(0);
    SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
    Date stringDate = simpledateformat.parse(date, pos);
    return stringDate;

}

From source file:Main.java

public static String getNextDateByMonth(String s, int i) {

    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
    java.util.Date date = simpledateformat.parse(s, new ParsePosition(0));
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//from   www.j a v a  2s.c  o m
    calendar.add(2, i);
    date = calendar.getTime();
    s = simpledateformat.format(date);
    return s;
}

From source file:Main.java

public static String getNextDateByNum(String s, int i) {

    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
    java.util.Date date = simpledateformat.parse(s, new ParsePosition(0));
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//  w  ww.j  a va 2  s  .c o m
    calendar.add(5, i);
    date = calendar.getTime();
    s = simpledateformat.format(date);
    return s;
}