Developer2as Developer2as - 3 months ago 8x
Java Question

Timestamp converter

I tried to create an timestamp to Date and Date to timestamp converter in java. the converter logic works perfectly and I displayed it on a console window. But this works not correctly.

If I type this timestamp into my console window:

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

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

First time I call a mehtod which required the timestamp format like above.

LongStamp = LongStamp/1000;


} else {


If match, the method logic.convertedTime(xx) is calling. ( See class convertLogic)

Here is my code:

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.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 + "";


And I get following output:

Datum: 13.10.2016 20:0:0

But I want or I need all zeros from the Hour,Minutes and seconds like this :

13.10.2016 20:00:00

Does anybody know an solution?

Thanks in advance!


Fix: Call your leadingZero method

After adding a leading zero you effectively remove it.

After padding with a leading zero, you call Integer.parseInt. That call generates a number, an int. The default format used when converting that to a String has no leading zero.

Change the data type of second to String.

Likewise, pass the result of myDate.get(Calendar.HOUR_OF_DAY) to your leadingZero method.

Strange results

I do not understand your input and results. If 1449946800000 is a count of milliseconds from the epoch of first moment of 1970 UTC, that is a date-time in 2015 not 2016:

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


You are working too hard. Use a date-time framework for this work.

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

Example code

First translate your count-from-epoch into an Instant object representing a moment on the timeline in UTC with a resolution up to nanoseconds.

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

Apply a time zone, Europe/Berlin. A ZoneId represents the time zone. We use that to generate a ZonedDateTime object.

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

Create a String to represent the value of our ZonedDateTime object.

No need to define a formatting pattern. The DateTimeFormatter class can automatically localize a date-time representation to a human language and cultural norms defined by a Locale. Your desired format is already known to be the medium-length format commonly used in Germany.

Understand that a time zone and a Locale are distinct and separate. One adjusts the meaning of the date-time to a wall-clock time of some geographic area on the globe. The other translates names of day or month, and determines comma versus period, and other such issues.

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

Dump to console.

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

input: 1449946800000 | instant: 2015-12-12T19:00:00Z | zdt: 2015-12-12T20:00+01:00[Europe/Berlin] | output: 12.12.2015 20:00:00