Cell formated like Time

Hi,


can you help with this, how to get value of cell which is formated like “Time”?

Problem is that when cell is formated like Time and I try to get value of that cell with method cell.getDateTimeValue() they always return “12-31-1899” date and time which is in cell. I want if cell is formated like “Time” to get just Time?

Thanks,
Bojan

Hi,

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

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

Below is a code example that illustrates how to get your desired time format. The code basically enters the current date into cell A1 and then format it as a Time.

I have also attached the output xlsx file and the screenshot for a reference.

Java


Workbook workbook = new Workbook();


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


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


//Enter current date and time

a1.putValue(DateTime.getNow());


//Format it as time

Style st = a1.getStyle();

st.setCultureCustom(“[$-409]h:mm\ AM/PM;@”);

st.setCustom(“[$-409]h:mm\ AM/PM;@”);

a1.setStyle(st);


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


Screenshot:

Thanks for answer but this is not what I want.


My problem is when I have some .xls or .xslx file and they contains cell which content is Time and I want to get value from this cell. How can I do this?

Hi,

Will you please elaborate more?

Please provide me your source xls/xlsx file and the expected output. You can create source file manually using Ms-Excel 2010.

Hi,

Please use the following code to find if the cell’s value is date time.

Java


if (cell.getType() == CellValueType.IS_DATE_TIME)
{
//access your value
}

Hi,


this is code how to get value and you have attached file.

public class TimeTest {

public void timeTest() throws Exception {
String path = “/home/emisia/Desktop/time.xls”;

Workbook workbook = new Workbook(path);

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

Cell cell = worksheet.getCells().get(0, 0);

if (cell.getType() == CellValueType.IS_DATE_TIME) {
DateTime dateTime = cell.getDateTimeValue();

System.out.println(dateTime.toString());

int i = cell.getIntValue();

System.out.println(i);
}

}

}

Output is “1899-12-31T22:30:00”, you can see that the Time is ok but they return and Date, expected value is just the Time value.
And it would be ok if the date and time can be taken separately.

Best reqards,
Bojan

Hi,

Please use Cell.getStringValue() property. It will return you correct time.

Please see the following code and its output.

Java


String filePath = “F:\Shak-Data-RW\Downloads\time.xls”;


Workbook workbook = new Workbook(filePath);


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


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


System.out.println(cell.getStringValue());


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


Output:
22:30