Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static int[] parseDate(String dateString) {
        try {
            DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
            formatter.setLenient(false);
            Date date = (Date) formatter.parse(dateString);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int[] dateInts = new int[3];
            dateInts[0] = cal.get(Calendar.DAY_OF_MONTH);
            dateInts[1] = cal.get(Calendar.MONTH);
            dateInts[2] = cal.get(Calendar.YEAR);

            return dateInts;
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not parse date:'" + dateString + "'. Date format is dd-MM-yyyy (ex: 31-12-2011).", e);
        }
    }
}