Solved

To get the current time as per locale passed

Posted on 2007-03-19
7
308 Views
Last Modified: 2010-03-31
Hi Readers,
Can you helpout on this query,
I want to display time as per the locale user passed.
Am providing a API call as follows
public static String getCurrentDateTime1(Locale alocale, String pattern){}

Here when when user passes locale object,
say for ex: " en_SG" i  need to display time as per singapore locale. If passed as "en_US" should disaply current time of US when this API is called.
As US locale will have dfferent timezone's, is it possible to have locale specific timing by just passing locale object to API method call.

Mine current Implementation:
public static String getCurrentDateTime1(Locale alocale, String pattern) {
        Calendar now = Calendar.getInstance(alocale);
        java.util.Date localeDate = now.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat (pattern,alocale);
        TimeZone tz=formatter.getTimeZone();
        System.out.println("======The Locale passed ========="+alocale);
        System.out.println("====== Timezone details========="+tz);
        String dateString = formatter.format(localeDate);
        return dateString;
}

Here am getting mine System timezone independent of locale i passed.

Can any one suggest in this iisue.

Thanks,

0
Comment
Question by:ramana903
  • 4
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 18754324
there is no relationship between locala and timezone, you'll need to specify timezone seperately.
0
 
LVL 92

Expert Comment

by:objects
ID: 18754331
>         TimeZone tz=formatter.getTimeZone();

that will return the default system tz
you need to instead set the tz

public static String getCurrentDateTime1(Locale alocale, Timezone tz, String pattern) {
        Calendar now = Calendar.getInstance(alocale);
        java.util.Date localeDate = now.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat (pattern,alocale);
        formatter.setTimezone(tz);
0
 

Author Comment

by:ramana903
ID: 18754366
Hi Genius,

If this is the case to pass the locale and timezone, what the exact use of the locale object here.
I i use
Calendar now = Calendar.getInstance(alocale);

it should get the calender object to specific loacle with default locale as i passed, right?

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 18754442
thats correct, but the locale is just used for display purpoise
it uses the default timezone

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html#getInstance(java.util.Locale)
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 18754444
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question