Convert Current date to integer

前端 未结 12 879
轮回少年
轮回少年 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:37

    tl;dr

    Instant.now().getEpochSecond()  // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
    

    Details

    As others stated, a 32-bit integer cannot hold a number big enough for the number of seconds from the epoch (beginning of 1970 in UTC) and now. You need 64-bit integer (a long primitive or Long object).

    java.time

    The other answers are using old legacy date-time classes. They have been supplanted by the java.time classes.

    The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

    Instant now = instant.now() ;
    

    You can interrogate for the number of milliseconds since the epoch. Beware this means a loss of data, truncating nanoseconds to milliseconds.

    long millisecondsSinceEpoch = now.toEpochMilli() ;
    

    If you want a count of nanoseconds since epoch, you will need to do a bit of math as the class oddly lacks a toEpochNano method. Note the L appended to the billion to provoke the calculation as 64-bit long integers.

    long nanosecondsSinceEpoch = ( instant.getEpochSecond() * 1_000_000_000L ) + instant.getNano() ;
    

    Whole seconds since epoch

    But the end of the Question asks for a 10-digit number. I suspect that means a count of whole seconds since the epoch of 1970-01-01T00:00:00. This is commonly referred to as Unix Time or Posix Time.

    We can interrogate the Instant for this number. Again, this means a loss of data with the truncation of any fraction-of-second this object may hold.

    long secondsSinceEpoch = now.getEpochSecond() ;  // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
    

    About java.time

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

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

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

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

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

    On my Java 7, the output is different:

    Integer : 1293732698
    Long : 1345618496346
    Long date : Wed Aug 22 10:54:56 MSK 2012
    Int Date : Fri Jan 16 02:22:12 MSK 1970
    

    which is an expected behavior.

    It is impossible to display the current date in milliseconds as an integer (10 digit number), because the latest possible date is Sun Apr 26 20:46:39 MSK 1970:

    Date d = new Date(9999_9999_99L);
    System.out.println("Date: " + d);
    
    Date: Sun Apr 26 20:46:39 MSK 1970
    

    You might want to consider displaying it in seconds/minutes.

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

    If you need only an integer representing elapsed days since Jan. 1, 1970, you can try these:

    // magic number=
    // millisec * sec * min * hours
    // 1000 * 60 * 60 * 24 = 86400000
    public static final long MAGIC=86400000L;
    
    public int DateToDays (Date date){
        //  convert a date to an integer and back again
        long currentTime=date.getTime();
        currentTime=currentTime/MAGIC;
        return (int) currentTime; 
    }
    
    public Date DaysToDate(int days) {
        //  convert integer back again to a date
        long currentTime=(long) days*MAGIC;
        return new Date(currentTime);
    }
    

    Shorter but less readable (slightly faster?):

    public static final long MAGIC=86400000L;
    
    public int DateToDays (Date date){
        return (int) (date.getTime()/MAGIC);
    }
    
    public Date DaysToDate(int days) {
        return new Date((long) days*MAGIC);
    }
    

    Hope this helps.

    EDIT: This could work until Fri Jul 11 01:00:00 CET 5881580

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

    Your Problem is because of getTime() . it always return following.

    Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

    Because the max integer value is less then the return value by getTime() that why is showing wrong result.

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

    Probably You can not, Long is higher datatype than Integer.

    or this link might help you

    http://www.velocityreviews.com/forums/t142373-convert-date-to-integer-and-back.html

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

    Try This

    Calendar currentDay= Calendar.getInstance();
    int currDate= currentDay.get(Calendar.DATE);
    int currMonth= currentDay.get(Calendar.MONTH);
    int currYear= currentDay.get(Calendar.YEAR);
    System.out.println(currDate + "-" +  currMonth + "-" + currYear);
    

    an alternative way using LocalDate.

    LocalDate today = LocalDate.now();
    int currentDate= today.getDayOfMonth();
    int currentMonth= today.getMonthValue();
    int currentYear= today.getYear()
    
    0 讨论(0)
提交回复
热议问题