简体   繁体   中英

String to Long in Java

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

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

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 eg 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

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) .

long永远无法格式化。只能格式化String 。如果需要使用long值,则可以使用Long.parseLong方法轻松地将其解析为long

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM