karthik prasad
karthik prasad

Reputation: 728

Android DateFormatter print Eastern Daylight Time instead of EDT

So Im trying to print the string "Eastern Daylight Time" instead of EDT . This should be dynamic and not hardcoded. Looking into DateFormatter class did not lead me to an answer that worked.

Here was an example that allows me to format but did not lead me to my specific answer.

I am getting the date back in the following format -

2013-06-08T00:00:00-04:00

Here are somethings that I have tried -

1)

 String dateString = changeFormatDateStringWithDefaultTimeZone(paymentConfirmation.getTransactionDate(),
                "yyyy-MM-dd'T'HH:mm:ssZ",
                "M/d/yyyy hh:mm a zz");



 public static String changeFormatDateStringWithDefaultTimeZone(String value, String ip_format, String op_format) {
        if (value == null)
            return null;

        try {
            SimpleDateFormat opSDF = new SimpleDateFormat(op_format, Locale.US);
            opSDF.setTimeZone(TimeZone.getDefault());

            SimpleDateFormat inSDF = new SimpleDateFormat(ip_format, Locale.US);

            Date date = inSDF.parse(value);
            return(opSDF.format(date));

        } catch (Exception e) {
            Log.e("Err", "Failed to convert time "+value);
            e.printStackTrace();
        }
        return null;
    }

2)

 Date today = Calendar.getInstance().getTime();
 String todayString = DateUtils.convertDateToStringWithTimeZone(today);


 public static String convertDateToStringWithTimeZone(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String dateString = df.format(date);
        dateString += " " + TimeZone.getDefault().getDisplayName(false, TimeZone.LONG);
        return dateString;
    }

These always print timezone as EDT and I want the string Eastern Daylight Time. Can anyone help me out with this?

Upvotes: 0

Views: 475

Answers (1)

Meno Hochschild
Meno Hochschild

Reputation: 44061

Okay, based on your last edit of the question, the solution should be like this:

case 1)

The output pattern should be changed to "M/d/yyyy hh:mm a zzzz" (note the count of z-symbols to enforce the full zone name). Depending on the date and the underlying timezone, the formatter SimpleDateFormat will automatically determine if the daylight or the standard name is to be used.

case 2)

Use TimeZone.getDefault().getDisplayName(true, TimeZone.LONG) to enforce the long daylight name. If your default timezone is "America/New_York" then such an expression should print "Eastern Daylight Time". Note that the boolean parameter has been changed to true.

Upvotes: 1

Related Questions