Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    public static Date parse(String dateFormatted, String dateFormat) {
        return parse(dateFormatted, dateFormat, false);
    }

    public static Date parse(String dateFormatted, String dateFormat, boolean useUtc) {
        return parse(dateFormatted, new SimpleDateFormat(dateFormat), useUtc);
    }

    public static Date parse(String dateFormatted, SimpleDateFormat dateFormat) {
        return parse(dateFormatted, dateFormat, false);
    }

    public static Date parse(String dateFormatted, SimpleDateFormat dateFormat, boolean useUtc) {
        Date date = null;
        if (!dateFormatted.isEmpty()) {
            try {
                if (useUtc) {
                    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
                }
                date = dateFormat.parse(dateFormatted);
            } catch (Exception e) {
                throw new RuntimeException(
                        "Error parsing the dateFormatted: " + dateFormatted + " pattern: " + dateFormat.toPattern(),
                        e);
            }
        }
        return date;
    }
}