How to insert Date into cell

Hi,

I have a spreadsheet where some cells have date format. In my Java class I want to insert “2012-04-19” into those cells. Could you please suggest what is the better way to do it. I assume I need to convert it to Date. Also, I only need to insert date, without time.

Thanks!

Hi,

Thanks for your posting and using Aspose.Cells for Java.

Please download and use the latest version:
Aspose.Cells for Java (Latest Version)


You can add date and time value and format them according to your needs. For this, you will have to use the Style object and Style.Custom property.

Please see the following code. I have also attached the output file.

I have also attached the screenshot for your reference.

Java



Workbook workbook = new Workbook();


Worksheet worksheet = workbook.getWorksheets().get(0);


Cell a1 = worksheet.getCells().get(“A1”);


Calendar c = Calendar.getInstance();


a1.putValue(c.getTime());



Style a1Style = a1.getStyle();


a1Style.setCustom(“yyyy/mm/dd”);

a1Style.setCultureCustom(“yyyy/mm/dd”);


a1.setStyle(a1Style);


worksheet.autoFitColumns();


workbook.save(“output.xlsx”);


Screenshot:

Hi,

Thanks a lot for your reply. In my case I don’t need to do formatting through Java since I get spreadsheet from user with already formatted cells. I also was using Calendar object to set date. It gets formatted properly. But when I select a cell, I also see the time, same way as on the screenshot provided above. I was wondering whether there is a way to not have the time part only date "2012-04-19"

Thanks,
Olga

Hi,

Yes, it is possible, just remove the time part.

In the code below, I get the current date in variable d, then I remove the time part and get it in d2 and insert d2 in cell A1.

As shown in screenshot, now, if you will select A1, you will not see any time part.

Java


Workbook workbook = new Workbook();


Worksheet worksheet = workbook.getWorksheets().get(0);


Cell a1 = worksheet.getCells().get(“A1”);


Calendar c = Calendar.getInstance();


Date d = c.getTime();


//Create a current date without any time part

Date d2= new Date(d.getYear(), d.getMonth(), d.getDay());


//Insert the d2

a1.putValue(d2);


Style a1Style = a1.getStyle();


a1Style.setCustom(“yyyy/mm/dd”);

a1Style.setCultureCustom(“yyyy/mm/dd”);


a1.setStyle(a1Style);


worksheet.autoFitColumns();


workbook.save(“output.xlsx”);

Screenshot:

Hi,

Thanks a lot for a quick reply. It seems like now the date inserted is off by 15 days.

Olga

Hi,

You are right. Please use this code now. This time, I have formatted the date into string and then from that string created a new date object which has no time units.

Java


Workbook workbook = new Workbook();


Worksheet worksheet = workbook.getWorksheets().get(0);


Cell a1 = worksheet.getCells().get(“A1”);


Calendar c = Calendar.getInstance();


Date d = c.getTime();


//Create a current date without any time part


DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd”);


Date d2 = new Date(dateFormat.format(c.getTime()));


//Insert the d2

a1.putValue(d2);



Style a1Style = a1.getStyle();


a1Style.setCustom(“yyyy/mm/dd”);

a1Style.setCultureCustom(“yyyy/mm/dd”);


a1.setStyle(a1Style);


worksheet.autoFitColumns();


workbook.save(“F:\Shak-Data-RW\Downloads\output.xlsx”);