1

My requirement:

I get value in EpochMillis

public void Method(){
    //long val = epoch time in millis
    //Format the (val)epochtime in MMddHHmmss and set ZoneOffset.UTC
    //long formattedTime = formatted time;
    obj.setTime(formattedTime);
}

The final objective is to get the formatted time in the above mentioned format and result should be in "long" type

I can process the expected data, but results in String, while converting back to Long, leading zeros are lost. (i want to retain the length as 10 - as per the format)

Can someone help ?

I tried below

final long timeInMillis = Instant.now().toEpochMilli();`
final Instant instant = Instant.ofEpochMilli(Long.valueOf(timeInMillis));`
final String formattedDate = DateTimeFormatter.ofPattern("MMddHHmmss").withZone(ZoneOffset.UTC).format(instant);

Here I could get only String, I expect long which is formatted (10 in length)

Edit1: Or I need any other idea. I need to set the time like 0803072816 [MM-dd-HH-mm-ss] using Setter Method which accepts only Long value. Long in milliseconds without formatting also fine either formatter long value or unformatted time in milliseconds, in both cases length should be exactly = 10

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
msa
  • 31
  • 1
  • 6
  • I don't get it right. Are you trying to get a `long` variable in the end with leading zeros (not possibe), or a formatted string with leading zeros? – hellow Aug 03 '18 at 09:22
  • 1
    Possible duplicate of [Left padding a String with Zeros](https://stackoverflow.com/questions/4469717/left-padding-a-string-with-zeros) – hellow Aug 03 '18 at 09:22
  • Leading zeros are not part of a mathematical number. It's *decoration* and as such only present in a representation like `String`. Use a **formatter** to display a number with leading zeros. Use a custom parser if you want to parse the leading zeros and remember them. As said, a number itself has no leading zeros and thus they are *lost*. – Zabuzard Aug 03 '18 at 09:34
  • Your object shouldn’t use a `long` for a date and time. Just as you shouldn’t use an `int` for holding a character or a `char` for holding a number. It’s wrong and it’s bound to confuse and can easily lead to errors. Use a date-time class from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/), like `Instant`. Then format it into the appropriate string for the user if and when you need to display it. – Ole V.V. Aug 04 '18 at 19:48

4 Answers4

3

For a computer, a long number like 00012 (in memory) is the same as 12. It doesn't care about leading 0.

As long as I know you are not a computer. So you don't read computer number but visual representation of this number. This representation is a String and can have leading 0.

What ever you do with your long value, it's perfectly normal the leading 0 are omitted. The only way to get those 0 is to use a String formatter whenever you need to display it.

For example: String.format("%010d", myNumber)

Edit: I wrote "00012 (in memory)" to not confuse it with the conversion of the string "00012" to long, which is the octal representation of the number 10

jhamon
  • 3,346
  • 3
  • 23
  • 35
  • `It doesn't care about leading 0` I see what you mean, but it could be confusing since writing such a number in code will make it be parsed as an octal number. – Aaron Aug 03 '18 at 09:34
  • Thanks, I added a note about that – jhamon Aug 03 '18 at 09:41
2

You cannot because a long does not have a leading zero. A long is supposed to store integers (the mathematical concept, not int).

A string of characters like 05 is not an integer, 5 is. What you can do is format a long that holds 5 with a leading zero when you print it, see e.g. java.util.Formatter.

Are you sure you even want to have a long/an integer? What do you want to do with it? Copied from: Parse String to long with leading zero

2

So you have something like

String s = "0128115900";
long n = Long.parse(s, 10); // 128115900L
String t = String.format("%010d", n); // "0128115900"

In most programming languages numbers have no fixed 0-padded width, only in SQL there is a width, for "storage."

And - by the way -:

long n = 0128115900L; // Error, octal number with digits 0-7

actually means something else: a preceding 0 causes the number to be in base 8, where every digit represents 3 bits, just as a preceding 0x if for base 16.

So to recap:

A long/int always has no width property, and could be assumed to be preceded by any zeroes, but its toString() string representation gives the shortest version.

To receive a string representation of some width, at the point of output use String.format("%010d", n) / System.out.printf("%010d%n", n).

Community
  • 1
  • 1
Joop Eggen
  • 96,344
  • 7
  • 73
  • 121
0

long could never be formatted.Only String is possible to format.You can parse the string to long easily By method Long.parseLong if you need its long value.