Solved

To get the current time as per locale passed

Posted on 2007-03-19
7
306 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now