Document Solutions for Excel, Java Edition | Document Solutions
In This Topic
    Use JDK 8 Date Time API
    In This Topic

    With the introduction of the new date and time libraries and robust APIs in Java 8, the legacy date and calendar APIs (java.util.Date and java.util.Calendar application programming interfaces) have been migrated in order to support JSR310 implementations, handle concurrency issues and ensure thread safety while working with Java applications. The new Date and Time application programming interfaces are standardized by ISO (International Organization for Standardization) keeping in mind the consistency models for important entities like the date, time, duration and period.

    DsExcel Java provides extensive support for JDK 8 and hence the configuration of the new JDK 8 Date Time API along with the new libraries, packages (including java.time, java.time.chrono, java.time.format, java.time.temporal and java.time.zone) and subpackages (LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration etc). All the new classes are extensible, immutable, ensure enhanced thread safety and are self-sufficient to cater to all the Java Date and Time API requirements; thus eliminating the need for any third-party APIs like Joda-Time while working with Java applications. Using DsExcel Java, users can handle all the new date and time APIs while working in Java Integrated Development Environments with Java Development Kit 8 or higher installed on their systems.        

    Refer to the following example code in order to use JDK 8 Date and Time APIs in DsExcel Java.

    Java
    Copy Code
            
    // Initialize workbook
    Workbook workbook = new Workbook();
            
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    // Fetch the cell range A1
    IRange a1 = worksheet.getRange("A1");
    
    /* Java 8 introduces a new package java.time which contains lots of new
       date/time types and sub-packages to support JSR310.
       DsExcel can handle these new types when working with Java 8 or upper*/
    
    // Setting Cell A1 date time value
    a1.setValue(java.time.LocalDateTime.now());
    
    // Get cell's date time value
    // LocalDateTime java8Date = (java.time.LocalDateTime)a1.getValue();
    
    // Formatting A1 cell
    a1.setNumberFormat("m/d/yyyy h:mm");
            
    // Setting column "A" width
    a1.setColumnWidth(30);
    
    // Saving workbook
    workbook.save("7-SetJDK8DatetimeValue.xlsx");