Example usage for java.text ParsePosition ParsePosition

List of usage examples for java.text ParsePosition ParsePosition

Introduction

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

Prototype

public ParsePosition(int index) 

Source Link

Document

Create a new ParsePosition with the given initial index.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    System.out.println(numberFormat.parse("123", new ParsePosition(0)));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    System.out.println(numberFormat.parseObject("123", new ParsePosition(0)));
}

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 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);//from   ww w. jav a  2  s .c o  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: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 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);/* w ww .ja v  a2 s  .  c  o  m*/

    // 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) {
    // Parse a string to decimal number
    String str = "qq1,234.567";
    String pattern = "#,###.###";
    formatter.applyPattern(pattern);//from  w w w . j  a  v  a2s. c  om

    // Create a ParsePosition object to specify the first digit of
    // number in the string. It is 1 in "qq1,234.567"
    // with the index 2.
    ParsePosition pp = new ParsePosition(2);

    Number numberObject = formatter.parse(str, pp);

    double value = numberObject.doubleValue();
    System.out.println("Parsed Value  is " + value);
}

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;
        }/* w w w.  j av a  2s.com*/
        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  www .  j a  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:org.apache.hadoop.chukwa.database.Aggregator.java

public static void main(String[] args) {
    long startTime = 0;
    long endTime = 0;
    long aggregatorStart = Calendar.getInstance().getTimeInMillis();
    long longest = 0;
    if (args.length >= 4) {
        ParsePosition pp = new ParsePosition(0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String buffer = args[0] + " " + args[1];
        Date tmp = format.parse(buffer, pp);
        startTime = tmp.getTime();//from  w ww. j  a v a2s  .  c  o m
        buffer = args[2] + " " + args[3];
        pp = new ParsePosition(0);
        tmp = format.parse(buffer, pp);
        endTime = tmp.getTime();
    }
    String longQuery = null;
    log.info("Aggregator started.");
    String cluster = System.getProperty("CLUSTER");
    if (cluster == null) {
        cluster = "unknown";
    }
    db = new DatabaseWriter(cluster);
    String queries = Aggregator
            .getContents(new File(System.getenv("CHUKWA_CONF_DIR") + File.separator + "aggregator.sql"));
    String[] query = queries.split("\n");
    while (startTime <= endTime) {
        for (int i = 0; i < query.length; i++) {
            if (query[i].equals("")) {
            } else if (query[i].indexOf("#") == 0) {
                log.debug("skipping: " + query[i]);
            } else {
                Aggregator dba = new Aggregator();
                long start = Calendar.getInstance().getTimeInMillis();
                try {
                    if (startTime != 0 && endTime != 0) {
                        dba.process(startTime, startTime, query[i]);
                    } else {
                        dba.process(query[i]);
                    }
                } catch (Throwable e) {
                    log.error("Invalid query:" + query[i]);
                }
                long end = Calendar.getInstance().getTimeInMillis();
                long duration = end - start;
                if (duration >= longest) {
                    longest = duration;
                    longQuery = query[i];
                }
            }
        }
        startTime = startTime + 5 * 60000;
    }
    db.close();
    long aggregatorEnd = Calendar.getInstance().getTimeInMillis();
    log.info("Longest running query: " + longQuery + " (" + (double) longest / 1000 + " seconds)");
    log.info("Total running time: (" + (double) (aggregatorEnd - aggregatorStart) / 1000 + " seconds)");
    log.info("Aggregator finished.");
}