简体   繁体   English

将long转换为int

[英]casting long to int

I have one variable which gets current time and stored in long. 我有一个获取当前时间并长时间存储的变量。 But I have to convert it to int as it is the requirement. 但是我必须将其转换为int ,这是必需的。 So what i am doing is converting the val with (int) and storing inside the int. 所以我在做什么是用(int)转换val并存储在int内部。

Val = 1355399741522 (long)
Int Val = -1809346991 (after casting to int)

After Casting from int to long -> Val = -1809346991 //TESTED

Now my question is if I want to convert this int again back to long than definitely it will not going to work for me. 现在,我的问题是,如果我想再次将此int转换回long,肯定不会对我有用。 That i already tested. 我已经测试过了。 But I want alternate solution for this. 但是我想要替代解决方案。

NOTE - I don't want to store long. 注意-我不想长时间存储。 As its the requirement. 作为其要求。 And I am using below function to convert the long to time 我正在使用以下功能将长时间转换为时间

public static String convertToTime(final long date) {
        String time = null;
        final SimpleDateFormat bartDateFormat1 = new SimpleDateFormat("HH");
        final SimpleDateFormat bartDateFormat2 = new SimpleDateFormat("mm");
        final Date fomDat = new Date(date);
        final int hour = Integer.parseInt(bartDateFormat1.format(fomDat));
        final int min = Integer.parseInt(bartDateFormat2.format(fomDat));
        time = pad(hour) + ":" + pad(min);
        return time;
    }

If anyone has any idea please kindly guide me or provide any alternative. 如果有人有任何想法,请指导我或提供其他选择。

If you have a long that contains a value greater than Integer.MAX_VALUE or less than Integer.MIN_VALUE , and you cast it to an int and then back to a long , you won't get the original value back again. 如果您的long包含的值大于Integer.MAX_VALUE或小于Integer.MIN_VALUE ,并且将其Integer.MIN_VALUE转换为int然后返回long ,则不会再次获得原始值。

If you have a "requirement" to do that, then your requirement is not implementable. 如果您有“要求”来执行此操作,那么您的要求将无法实现。 What you are trying to do is a mathematical impossibility. 您试图做的是数学上的不可能。

(You should also consider the possibility that you have misunderstood the requirement ...) (您还应该考虑误解了需求的可能性...)

When time is stored as an int it's usually in seconds because milli-seconds will not fit. 将时间存储为int ,通常以秒为单位,因为毫秒不适合。 If you do this 如果你这样做

long timeInMs = 1355399741522L;
int timeInSec = (int) (timeInMs / 1000); // now 1355399741
long timeInMs2 = timeInSec * 1000L; // now 1355399741000L

You can convert the long to String and create an Integer(String) object. 您可以将long转换为String并创建一个Integer(String)对象。 but since the Long.MAX_VALUE(2^63-1) is much more than the Integer.MAX_VALUE(2^31 - 1), you have to make a compromise on the length of the long variable if that exceeds the maximum length of the integer type. 但由于Long.MAX_VALUE(2 ^ 63-1)比Integer.MAX_VALUE(2 ^ 31-1)大得多,因此,如果long变量的长度超过了最大长度,则必须妥协。整数类型。

public static String convertToTime(final long date) {
        String time = null;
        final SimpleDateFormat bartDateFormat1 = new SimpleDateFormat("H:m");
        time = bartDateFormat1.format( date );
        return time;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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