Solved

To get the current time as per locale passed

Posted on 2007-03-19
7
309 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 500 total points
ID: 18754444
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 89
Glassfish admin console not working 1 88
jmeter usage 4 63
Java Inharitance subclass/child extends, reference variable from base/parent class 9 72
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Suggested Courses

739 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