Convert Current date to integer

前端 未结 12 880
轮回少年
轮回少年 2020-12-05 04:35

I want to convert the current date to integer value. By default, it returns long. When I try to convert long to integer, and afterwards I convert the integer value to date,

相关标签:
12条回答
  • 2020-12-05 04:46

    The issue is that an Integer is not large enough to store a current date, you need to use a Long.

    The date is stored internally as the number of milliseconds since 1/1/1970.

    The maximum Integer value is 2147483648, whereas the number of milliseconds since 1970 is currently in the order of 1345618537869

    Putting the maximum integer value into a date yields Monday 26th January 1970.

    Edit: Code to display division by 1000 as per comment below:

        int i = (int) (new Date().getTime()/1000);
        System.out.println("Integer : " + i);
        System.out.println("Long : "+ new Date().getTime());
        System.out.println("Long date : " + new Date(new Date().getTime()));
        System.out.println("Int Date : " + new Date(((long)i)*1000L));
    
    Integer : 1345619256
    Long : 1345619256308
    Long date : Wed Aug 22 16:37:36 CST 2012
    Int Date : Wed Aug 22 16:37:36 CST 2012
    
    0 讨论(0)
  • 2020-12-05 04:46

    Java Date to int conversion:

    public static final String DATE_FORMAT_INT = "yyyyMMdd";
    
    public static String format(Date date, String format) {
        return isNull(date) ?  
        null : new SimpleDateFormat(format).format(date);
    }
    
    public static Integer getDateInt(Date date) {
        if (isNull(date)) {
            throw new IllegalArgumentException("Date must not be NULL");
        }
    
        return parseInt(format(date, DATE_FORMAT_INT));
    }
    
    0 讨论(0)
  • 2020-12-05 04:49

    Simple really create a long variable that represents a default start date for your program Get the date to another long variable. Then deduct the long start date and convert to a integer voila To read and convert back just add rather than subtract. obviously this is dependant on how large a date range you require.

    0 讨论(0)
  • 2020-12-05 04:52

    Do you need something like this(without time)?

    public static Integer toJulianDate(Date pDate) {
    if (pDate == null) {
      return null;
    }
    Calendar lCal = Calendar.getInstance();
    lCal.setTime(pDate);
    int lYear = lCal.get(Calendar.YEAR);
    int lMonth = lCal.get(Calendar.MONTH) + 1;
    int lDay = lCal.get(Calendar.DATE);
    int a = (14 - lMonth) / 12;
    int y = lYear + 4800 - a;
    int m = lMonth + 12 * a - 3;
    return lDay + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
    }
    
    0 讨论(0)
  • 2020-12-05 04:55

    I've solved this as is shown below:

        long year = calendar.get(Calendar.YEAR);
        long month = calendar.get(Calendar.MONTH) + 1;
        long day = calendar.get(Calendar.DAY_OF_MONTH);
        long calcDate = year * 100 + month;
        calcDate = calcDate * 100 + day;
        System.out.println("int: " + calcDate);
    
    0 讨论(0)
  • 2020-12-05 05:02

    In order to get current date as integer(10 digit number), you need to divide the long returned from new Date().getTime() by 1000.

    This will be in int range and is good until 18 Jan 2038.

    0 讨论(0)
提交回复
热议问题