Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    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);

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

    }

}