Solved

To get the current time as per locale passed

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 42
servlet example 17 51
Convert from a json string array to a Java object 3 51
Java class and jar 3 21
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

839 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