如何从Java的时区获得一天



我的输入将是时区

String timeZone = currentTimeZone.getID();
timezone = America/Los_Angeles

out put应该是那天属于该时区的一天。

Monday

这是我到目前为止尝试过的。

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); 
Date datetime = new Date(); 
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); 
System.out.println("Asia/Calcutta "+ sdf.format(datetime));
Calendar calendar = Calendar.getInstance();
Calendar LATime = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"));
LATime.setTimeInMillis(calendar.getTimeInMillis());
int year = LATime.get(Calendar.YEAR);
int month = LATime.get(Calendar.MONTH);
int date = LATime.get(Calendar.DATE);
int dayOfWeek = calender.get(Calendar.DAY_OF_WEEK);

尝试一下

您可以使用其他答案,该答案可以为您提供整数,然后可以相应地将其映射到一周的一天,也可以使用SimpleDateFormat来打印这样的一天:

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date datetime = new Date();
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println("America/Los_Angeles " + sdf.format(datetime));

希望这会有所帮助。

>  String[] days =
>                 {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
>         Calendar calendar = Calendar.getInstance();
>         Calendar LATime = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"));
>         LATime.setTimeInMillis(calendar.getTimeInMillis());
>         int year = LATime.get(Calendar.YEAR);
>         int month = LATime.get(Calendar.MONTH);
>         int date = LATime.get(Calendar.DATE);
>         int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
>         System.out.println(days[dayOfWeek -1]);

我们还可以设置calendar.setfirstdayofweek();默认是本周开始的星期日。

相关内容

  • 没有找到相关文章

最新更新