Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 The MIT License (MIT)
    
 Copyright (c) 2014 Robert C. Robinson
    
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
    
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
    
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 */

import java.text.DateFormat;
import java.text.ParseException;

import java.util.Date;
import java.util.Locale;

import android.content.Context;

public class Main {
    public static Date FormatDate(String sRecDate, Context c) {
        DateFormat df = DateFormat.getDateTimeInstance();
        DateFormat dfS = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
        DateFormat dfM = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        DateFormat dfL = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
        DateFormat dfF = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);

        DateFormat dfNoTime = DateFormat.getDateInstance();
        DateFormat dfSNoTime = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
        DateFormat dfMNoTime = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH);
        DateFormat dfLNoTime = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH);
        DateFormat dfFNoTime = DateFormat.getDateInstance(DateFormat.FULL, Locale.ENGLISH);
        DateFormat dfDevice = android.text.format.DateFormat.getDateFormat(c);

        Date dRecDate = null;

        try {
            dRecDate = dfDevice.parse(sRecDate);
        } catch (ParseException e) {
            try {
                dRecDate = df.parse(sRecDate);
            } catch (ParseException e2) {

                try {
                    dRecDate = dfS.parse(sRecDate);
                } catch (ParseException e3) {

                    try {
                        dRecDate = dfM.parse(sRecDate);
                    } catch (ParseException e4) {

                        try {
                            dRecDate = dfL.parse(sRecDate);
                        } catch (ParseException e5) {

                            try {
                                dRecDate = dfF.parse(sRecDate);
                            } catch (ParseException e6) {

                                try {
                                    dRecDate = dfNoTime.parse(sRecDate);
                                } catch (ParseException e7) {

                                    try {
                                        dRecDate = dfSNoTime.parse(sRecDate);
                                    } catch (ParseException e8) {

                                        try {
                                            dRecDate = dfMNoTime.parse(sRecDate);
                                        } catch (ParseException e9) {

                                            try {
                                                dRecDate = dfLNoTime.parse(sRecDate);
                                            } catch (ParseException e10) {

                                                try {
                                                    dRecDate = dfFNoTime.parse(sRecDate);
                                                } catch (ParseException e11) {

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return dRecDate;

    }
}