Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8040259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:50:21+00:00 2026-06-05T03:50:21+00:00

I have very irregular dates like: Mon, 9 Jan 2012 14:18:32 -0800 (GMT) 18

  • 0

I have very irregular dates like:

Mon, 9 Jan 2012 14:18:32 -0800 (GMT)

18 Oct 2006 00:32:03 -0000

Sat, 18 Nov 2006 19:52:23 UT

I made a really complex function to get it all done, it works but for the purpose of learning i want to do it again with using try and catch.
My question is, how can i deal with the timezone? You can see all the timezone’s in my function, however i also have sometime (… as a timezone for example and i don’t want to store all cases in a String array.

Also i want to use Joda-Time, (it can be localDate or dateTime, doesn’t matter that much to me).

public LocalDate handleDate(String date) {

        String[] days = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
        String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
                "Aug", "Sep", "Oct", "Nov", "Dec" };
        String[] years = { "2004", "2005", "2006", "2007", "2008", "2009",
                "2010", "2011", "2012" };

        // ORDER IS VERRY IMPORTANT!! ( MEST must be before EST for example)
        String[] timesZones = { "BST", "CET", "CEST", "CST", "CDT", "EDT",
                "GMT+00:00", "GMT", "IST", "MEST", "EST", "MET", "MDT", "PST",
                "PDT", "SAST", "UTC", "UT", "W. Europe Standard Time",
                "West-Europa (zomertijd)" };

        String origDate = date;

        String timeZone = "";
        String year = "";
        String month = "";
        String day = "";
        int hours = 0;
        int minutes = 0;
        int seconds = 0;

        // is it valid?
        date = trim(date);
        if (date.equals("")) {
            return null;
        }

        // first delete the comma that comes mostly after the day
        date = date.replaceAll(",", "");

        // remove the day
        for (int i = 0; i < days.length; i++) {
            if (date.contains(days[i])) {
                date = date.replace(days[i], "");
                break;
            }
        }

        // if(date.contains("23:27:17")) println(date);

        for (int i = 0; i < timesZones.length; i++) {
            // first check with '(' and ')'
            String target = "(" + timesZones[i] + ")";

            if (date.contains(target)) {
                timeZone = timesZones[i];
                date = date.replace(target, "");
                break;
            }

            // if not found check without '(' and ')'
            if (date.contains(timesZones[i])) {
                timeZone = timesZones[i];
                date = date.replace(timesZones[i], "");
                break;
            }
        }

        // get the month
        for (int i = 0; i < months.length; i++) {
            if (date.contains(months[i])) {
                month = months[i].toLowerCase(); // !must be lowercase
                // must be dutch on my pc
                if (month.equals("oct"))
                    month = "okt";
                if (month.equals("may"))
                    month = "mei";
                if (month.equals("mar"))
                    month = "mrt";

                date = date.replace(months[i], "");
                break;
            }
        }

        // get the year
        for (int i = 0; i < years.length; i++) {
            if (date.contains(years[i])) {
                year = years[i];
                date = date.replace(years[i], "");
                break;
            }
        }

        // get the time
        Pattern p = Pattern.compile("(\\d\\d):(\\d\\d):(\\d\\d)");
        Matcher m = p.matcher(date);
        if (m.find()) {
            // also fix the time, 00 is not allowed

            hours = Integer.parseInt(m.group(1));
            minutes = Integer.parseInt(m.group(2));
            seconds = Integer.parseInt(m.group(3));

            date = date.replaceAll("(\\d\\d:\\d\\d:\\d\\d)", "");
        }

        // get the time difference
        date = date.replace("+-", "+0"); // bug fix where data is incorrect ( 16
                                            // Sep 2007 23:27:17 +-200)

        p = Pattern.compile("[+|-]*(\\d\\d)\\d\\d");
        m = p.matcher(date);
        if (m.find()) {
            int timeDifferenceH = Integer.parseInt(m.group(1));

            date = date.replaceAll("([+|-]*\\d\\d\\d\\d)", "");
        }

        date = " " + date; // bug fix

        // get the day
        for (int i = 31; i >= 1; i--) {
            // first check for the ones that contains 2 digits (like 07)
            String d = nf(i, 2);
            if (date.contains(d)) {
                day = nf(i, 2);
                date = date.replace(d, "");
                break;
            }

            // check for 1 digit
            d = "" + i;
            if (date.contains(d)) {
                day = nf(i, 2);
                date = date.replace(d, "");
                break;
            }
        }

        // there should be nothing left except white space
        date = date.replace(" ", "");
        if (date.equals("") == false) {
            println("handleDate: problem with input\n" + date);
            println(origDate + "\n");
            println(year);
            println(month);
            println(day);
        }

        // String cleanDate = day + "/" + month + "/" + year + " " + nf(hours,
        // 2) + ":" + nf(minutes, 2) + ":" + nf(seconds, 2);
        // DateTimeFormatter formatter =
        // DateTimeFormat.forPattern("dd/MMM/yyyy HH:mm:ss");

        String cleanDate = year + "-" + month + "-" + day;
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MMM-dd");

        try {

            // DateTime dt = formatter.parseDateTime(cleanDate);
            // LocalDate dt = new LocalDate(cleanDate);
            // return dt.toLocalDate();
            // return dt;
            // return new LocalDate().parse(cleanDate, formatter);
            return formatter.parseLocalDate(cleanDate);
        } catch (IllegalArgumentException iae) {
            println("handleDate: Problem with formatting: " + cleanDate);
        }

        return null;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:50:22+00:00Added an answer on June 5, 2026 at 3:50 am

    The DateTimeFormatter class in JodaTime is quite strong and flexible in this, you should try Custom Formatters or Freaky Formatters

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very long integer sequences that look like this (arbitrary length!): 0000000001110002220033333 Now
I have very simple query like this: SELECT * FROM `all_conversations` WHERE `deleted_1` !=
I have very simple form, with input like this: <input id=my_id multiple=true type=file name=image_name[]
I have very simple select like this: SELECT * FROM table WHERE column1 IN
Maybe someone have idea for perl script with very strong irregular expression that match
I have very custom TableView like Add Contact View in iPhone's Contacts. I like
i have very serious problem if any tech expert can help...thank you in advance..
I have very simple piece of code. The goal is when i input four-digit
I have very very big html page/data. I need to fetch data under h1
I have very strange problem while I am submitting a practice problem on codechef.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.