1

我正在尝试在 java 中获取“TimeZoneOffset”,并参考这个,我实现了这个:

long unix_time_at_midnight;
DateFormat dateFormat= new SimpleDateFormat("MM/dd/yyyy");
Date d = new Date((unix_time*1000)/1000);
String d1 = dateFormat.format(d);
unix_time_at_midnight = Long.parseLong(d1);
int m=TimeZone.getOffset(unix_time_at_midnight) ;

我得到“TimeZone 类型的方法 getOffset(int, int, int, int, int, int) 不适用于参数(long)”。任何人都可以指导吗?

4

1 回答 1

1

The method Timezone.getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds); is an abstract class and one has to reference it from a implemented Timezone subclass.

You're trying to access a non-static method statically, and the compiler is matching it to the abstract method.

The solution you're seeking of is:

int m=TimeZone.getDefault().getOffset(unix_time_at_midnight) ;

EDIT: Looking on TimeZone on BlackBerry API, I see there is no getOffset(long date) method, but getOffset(int era, int year, int month, int day, int dayOfWeek, int millis).

A possible solution might be:

Date d = new Date((unix_time*1000)/1000)
TimeZone tz = TimeZone.getDefault();
Calendar c = Calendar.getInstance(tz);
c.setTime(d);
int m = tz.getOffset(1, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.DAY_OF_WEEK), c.get(Calendar.MILLISECOND));
于 2013-01-20T13:25:39.313 回答