![](/img/trans.png)
[英]How to convert a UTC timestamp to hour, minute to display on the graph in Java?
[英]How to convert a UTC timestamp to local day, hour, minute in Java?
然后给出时间戳1245613885,这是GMT中的时间戳
如何使用服务器的本地时区信息将其转换为Java中的年,日,小时,分钟值?
您可以使用java.util.Calendar
。 它提供了一个setTimeZone()
方法(这是多余的,因为它默认情况下已经选择了系统默认时区)。
long timestamp = 1245613885;
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.setTimeInMillis(timestamp * 1000);
int year = calendar.get(Calendar.YEAR);
int day = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
如果你想将它呈现在一个人类可读的日期字符串中,那么我建议使用SimpleDateFormat
。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(calendar.getTime());
System.out.println(dateString); // 2009-06-21 15:51:25
(根据我的时区GMT-4,输出是正确的)
import java.util.Calendar;
import java.util.TimeZone;
public class Example{
public static void main(String[] args){
long utcTimestamp = 1285578547L;
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
cal.setTimeInMillis(utcTimestamp * 1000);
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
System.out.println(cal.get(Calendar.DAY_OF_YEAR));
System.out.println(cal.get(Calendar.HOUR));
System.out.println(cal.get(Calendar.MINUTE));
}
}
我会使用像Joda Time这样的东西比JDK Date / Calendar类快得多,而且日期解析也没有线程安全问题(不是你的问题与日期解析有关)
基本上你只需要一个格式化程序来执行此操作
日期日期=新日期(1245613885L * 1000);
SimpleDateFormat formatter = new SimpleDateFormat('MM / dd / yyyy');
的System.out.println(formatter.format(日期));
Instant // Represent a moment in UTC.
.ofEpochMilli( 1_245_613_885L ) // Parse a count of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00:00Z.
.atZone( // Adjust from UTC to the wall-clock time used by the people of a particular region (a time zone).
ZoneId.of( "Asia/Tokyo" ) // Or "America/Chicago" etc.
) // Returns a `ZonedDateTime` object.
.format( // Generate text representing the value of our `ZonedDateTime` object.
DateTimeFormatter
.ofLocalizedDateTime( FormatStyle.FULL ) // Automatically localize the format and content of the string being generated.
.withLocale( Locale.CANADA_FRENCH ) // Or Locale.US etc.
) // Returns a `String` object.
jeudi 15 janvier1970à19h 00 min 13 s heure normale du Japon
现代方法使用java.time类,这些类在JSR 310采用时取代了糟糕的日期时间类。
如果输入1245613885
是自UTC 1970年第一个时刻以来的毫秒数,则解析为Instant
(UTC时刻)。
Instant instant = Instant.ofEpochMilli( 1_245_613_885L ) ;
使用标准ISO 8601格式生成表示该Instant
对象值的字符串。
String output = instant.toString() ;
从UTC调整到特定区域(时区)的人员使用的挂钟时间。
以Continent/Region
格式指定适当的时区名称 ,例如America/Montreal
, Africa/Casablanca
或Pacific/Auckland
。 切勿使用EST
或IST
等2-4字母缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
如果要使用JVM的当前默认时区,请求它并作为参数传递。 如果省略,代码变得模糊不清,因为我们不确定您是否打算使用默认值,或者如果您像许多程序员一样,不知道这个问题。
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
将ZoneId
应用于Instant
以获取ZonedDateTime
。
ZonedDateTime zdt = instant.atZone( z ) ; // Same moment as the `instant`, but different wall-clock time.
使用扩展的标准ISO 8601格式生成表示该ZonedDateTime
对象的值的字符串,以将该区域的名称附加在方括号中。
String output = zdt.toString() ;
对于其他格式,请指定自己的自定义格式设置模式,或让java.time自动进行本地化。 对于任一路由,请使用DateTimeFormatter
。 搜索Stack Overflow已经多次处理过了。
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"), Locale.US);
calendar.setTimeInMillis(1245613885 * 1000);
int year = calendar.get(Calendar.YEAR);
int day = calendar.get(Calendar.DAY_OF_YEAR);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
如果需要不同的时区,请更改传递给Calendar
对象构造函数的TimeZone
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.