Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**
     * Tries to parse a Date from Bitbucket's scraped HTML source code.
     *
     * @param datetime contents of datetime attribute of time tag
     *        For example 2014-06-06T22:08:03+03:00 or 2015-01-12T20:07:08.658593
     * @return Date or null if parsing fails
     */
    public static Date parseBitbucketDatetime(String datetime) {
        Date date;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        try {
            date = format.parse(datetime);
        } catch (ParseException e1) {
            format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
            try {
                date = format.parse(datetime);
            } catch (ParseException e2) {
                format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date = format.parse(datetime.substring(0, 10));
                } catch (ParseException | IndexOutOfBoundsException e3) {
                    // give up and hope these will be provided via API in the future :)
                    date = null;
                }
            }
        }
        return date;
    }
}