我试图在Java中创建一个时间戳到Date和Date to timestamp转换器。转换器逻辑完美运行,我将其显示在控制台窗口中。但这不能正常工作。

如果我在控制台窗口中输入此时间戳记:

1449946800000 (I get this timestamp from my calendar web plugin)

Date : 13.10.2016 20:00:00 (Timezone "Europe/Berlin ) = 1476381600000


第一次我调用一个mehtod,它需要像上面这样的时间戳格式。

      LongStamp = LongStamp/1000;

        logic.convertedTime(LongStamp);

        } else {
          .....

        }


如果匹配,则调用方法logic.convertedTime(xx)。 (请参见类convertLogic)

这是我的代码:

public class convertLogic {

/** TimeZone-Support**/
private static final String TIMEZONEBERLIN = "Europe/Berlin";


/**
 * convertedTime:  <br>
 * input timestamp to convert into date <br>
 * <strong>...</strong>
 * @param timestamp
 */
public void convertedTime(long timestamp) {

    TimeZone timeZoneBerlin = TimeZone.getTimeZone(TIMEZONEBERLIN);
    // System.out.println(timeZoneBerlin.getDisplayName());
    // System.out.println(timeZoneBerlin.getID());
    // System.out.println(timeZoneBerlin.getOffset(System.currentTimeMillis()));

    Calendar myDate = GregorianCalendar.getInstance();
    myDate.setTimeZone(timeZoneBerlin);
    myDate.setTimeInMillis(timestamp * 1000);

    int month = myDate.get(Calendar.MONTH) + 1;
    int second = Integer.parseInt(leadingZero(myDate.get(Calendar.SECOND)));


    System.out.println("Datum: " + myDate.get(Calendar.DAY_OF_MONTH) + "." + month + "." + myDate.get(Calendar.YEAR)
            + " " + myDate.get(Calendar.HOUR_OF_DAY) + ":" + myDate.get(Calendar.MINUTE) + ":"
            + second);

}



 /**
 * leadingZero for 2 digit values
 *
 * @param value
 * @return String
 */
private static String leadingZero(int value) {

    int testValue = value;

    if (testValue < 10) {

        return "0" + value;
    } else {

        return value + "";
    }

}


我得到以下输出:

 Datum: 13.10.2016 20:0:0


但是我想要或者我需要像这样的小时,分​​钟和秒中的所有零:

  13.10.2016 20:00:00


有人知道解决方案吗?

提前致谢!

最佳答案

修复:调用您的leadingZero方法

添加前导零后,可以有效地将其删除。

用前导零填充后,调用Integer.parseInt。该调用会生成一个数字,即int。将其转换为字符串时使用的默认格式没有前导零。

second的数据类型更改为String

同样,将myDate.get(Calendar.HOUR_OF_DAY)的结果传递给您的leadingZero方法。

结果奇怪

我不明白您的意见和结果。如果1449946800000是从1970 UTC的第一时刻的毫秒数开始的毫秒数,则这是2015年的日期时间而不是2016年的日期时间:


2015-12-12T19:00:00Z
2015-12-12T20:00+01:00[Europe/Berlin]


java.time

你太辛苦了为此工作使用日期时间框架。

java.time框架内置于Java 8及更高版本中。这些类取代了以前麻烦的日期时间类,例如java.util.Date.Calendarjava.text.SimpleDateFormat

要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。

许多java.time功能都被反向移植到ThreeTen-Backport中的Java 6和7,并进一步适应了ThreeTenABP中的Android。

范例程式码

首先将您的从纪元计数转换为Instant对象,该对象表示UTC中时间轴上的某个时刻,分辨率最高为nanoseconds

long input = 1_449_946_800_000L;
Instant instant = Instant.ofEpochMilli ( input );


应用时区Europe/BerlinZoneId代表时区。我们用它来生成一个ZonedDateTime对象。

ZoneId zoneId = ZoneId.of ( "Europe/Berlin" );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );


创建一个String来表示我们的ZonedDateTime对象的值。

无需定义格式模式。 DateTimeFormatter类可以自动将日期时间表示本地化为Locale定义的人类语言和文化规范。所需的格式已经众所周知,是德国常用的中等长度格式。

理解time zoneLocale是截然不同的。可以将日期时间的含义调整为全球某个地理区域的挂钟时间。另一个翻译日期或月份的名称,并确定逗号与句号之间的关系以及其他此类问题。

Locale locale = Locale.GERMANY;
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.MEDIUM ).withLocale ( locale );
String output = zdt.format ( formatter );


转储到控制台。

System.out.println ( "input: " + input + " | instant: " + instant + " | zdt: " + zdt + " | output: " + output );



  输入:1449946800000 |即时:2015-12-12T19:00:00Z | zdt:2015-12-12T20:00 + 01:00 [欧洲/柏林] |输出:12.12.2015 20:00:00

07-24 09:37
查看更多