java时间格式的转换以及EXCEL里面时间的读取

一、

/**
	 * @author SHUN
	 * 生成系统日期,并将其格式化返回字符串
	 * @return 
	 */
	public String generateDate(){
		  String guarantee_date = null;
		  Date generateDate = new Date();
	             DateFormat formater = new SimpleDateFormat("yyyyMMdd");
	             guarantee_date = formater.format(generateDate);
	         return guarantee_date;
	}

二、

/**
	 * @author SHUN
	 * @param cell  import org.apache.poi.ss.usermodel.Cell;
	 * 关于如何将Excel表格中的时间字符串的数字格式  转换成 格式化的时间字符串
	 * @return
	 */
	public String excelTime(Cell cell){
		  String guarantee_time = null;
	         if(DateUtil.isCellDateFormatted(cell)){
	             //用于转化为日期格式
	             Date d = cell.getDateCellValue();
//	             System.err.println(d.toString());
//	             DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	             DateFormat formater = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
	             guarantee_time = formater.format(d);
	                }
	         return guarantee_time;
	}

由于excel 里面的时间格式是自定义,所以javaPOI 读取出来的是一串数字,因此需要转换格式

         


三、

/**通过此方法再将被格式化了的时间字符串转换回标准时间类格式,再形成其他的时间
 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		         Date d = dateFormat.parse(excelTime(cell));
		         System.err.println(d.toString());


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中处理Excel中的日期格式,可以通过以下几个步骤来实现: 1. 使用POI库读取Excel文件,获取日期单元格中的值。 2. 判断单元格的格式是否为日期格式,可以使用Cell类的getCellType()方法和DataFormatter类的formatCellValue()方法来判断。 3. 如果单元格的格式为日期格式,可以使用DataFormatter类的formatCellValue()方法将日期格式转换为字符串格式。 4. 将字符串格式的日期转换Java中的日期格式,可以使用SimpleDateFormat类的parse()方法或者DateTimeFormatter类的parse()方法进行转换。 下面是一个示例代码,演示如何读取Excel中的日期格式,并将其转换Java中的日期格式: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Date; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelDateConverter { public static void main(String[] args) throws IOException, ParseException { // 读取Excel文件 File file = new File("test.xlsx"); FileInputStream fis = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(fis); fis.close(); // 获取第一个Sheet int sheetIndex = 0; DataFormatter dataFormatter = new DataFormatter(); LocalDate localDate; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); for (int i = 0; i < workbook.getSheetAt(sheetIndex).getPhysicalNumberOfRows(); i++) { Cell cell = workbook.getSheetAt(sheetIndex).getRow(i).getCell(0); if (cell != null) { // 判断单元格的格式是否为日期格式 if (DateUtil.isCellDateFormatted(cell)) { // 使用DataFormatter类将日期格式转换为字符串格式 String dateString = dataFormatter.formatCellValue(cell); // 使用SimpleDateFormat类将字符串格式的日期转换Java中的日期格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(dateString); System.out.println(date); // 使用Java8中的LocalDate类将字符串格式的日期转换Java中的日期格式 localDate = LocalDate.parse(dateString, dateTimeFormatter); System.out.println(localDate); } } } } } ``` 在上面的代码中,我们使用了POI库中的DataFormatter类来判断单元格的格式是否为日期格式,并将日期格式转换为字符串格式。然后,我们使用了SimpleDateFormat类和Java8中的LocalDate类将字符串格式的日期转换Java中的日期格式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千码君2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值