質問

I have web application with me. It is showing US time. I want it to show Indian time. I have tried setting property uing System.getProperty().setProperty("user.country","IN"); and System.getProperty().setProperty("user.variant","IN"); but still it is showing use time.

I even tried Locale.setDefault(new Locale("en", "IN")); but in vain.

When I do this

SimpleDateFormat format = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa -- z");
System.out.println("Date: "+format.format(new Date())); 

The result is Date: 02013.March.14 AD 12:29 AM -- GMT-08:00. what am I doing wrong.

It is a JSF 1.1 and Hibernate 3 project deployed in tomcat 6.

Thanks in advance.

役に立ちましたか?

解決 2

Add this line:

format.setTimeZone(TimeZone.getTimeZone("IST"));

他のヒント

Try this

    SimpleDateFormat f = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa -- z");
    f.setTimeZone(TimeZone.getTimeZone("IST"));
    System.out.println(f.format(new Date()));

Your question is confusing. Is it really US time? JSF defaults to UTC. If it is indeed really US time (e.g. EDT), then it should have explicitly been configured as such in timeZone attribute of all <f:convertDateTime> tags. All you needed to do would be to change "EDT" to "IST". But this is such an extremely obvious solution that I think that you're actually not running US time, but just UTC.

Your attempt to solve the problem is also confusing. You said and tagged that you're using JSF, but you're not attempting to solve it the JSF way, but the plain Java way. This is really not how you're supposed to convert date/time in JSF. You should use <f:convertDateTime> for the job and not SimpleDateFormat.

In any case, you need to configure all <f:convertDateTime> tags to explicitly set the timeZone attribute to IST.

<h:outputText value="#{bean.date}">
    <f:convertDateTime timeZone="IST" />
</h:outputText>

Note that if you were using JSF 2.x, then you could also have set a web.xml context parameter to apply this change applicationwide. See also Set a default time zone for f:convertDateTime.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top