Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

To get the current time as per locale passed

Posted on 2007-03-19
7
Medium Priority
?
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 18754444
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

715 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