繁体   English   中英

从 Excel 更改日期格式

[英]Change format of date from Excel

我写了一个 Java 文件处理程序,它从一个没有输入的 excel 文件中获取日期和日期。

excel 文件存储了一些人的考勤记录。 如果某一天没有条目,那么程序应该检查这一天是否是周末。 如果不是周末,则程序应包含姓名、日期和日期 output。

但是 output 的当前格式并不像我想要的那样:


No entry found for Shiv on Wed Jul 27 00:00:00 IST 2022
No entry found for Shiv on Thu Jul 28 00:00:00 IST 2022
No entry found for Shiv on Fri Jul 29 00:00:00 IST 2022
No entry found for Shiv on Mon Aug 01 00:00:00 IST 2022
No entry found for Shiv on Wed Aug 03 00:00:00 IST 2022
No entry found for Shiv on Thu Aug 04 00:00:00 IST 2022

我希望 output 日期的样式为 2022 年 8 月 1 日,星期一

我该如何实现?

以下是代码


import java.io.File;  
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Calendar;
import java.text.DateFormat;  
 
import java.util.Date;  
import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.Row;  
import org.apache.poi.xssf.usermodel.XSSFSheet;  
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
public class ExcelReader {
    public static void main(String [] args) {
        try {
            File file=new File("./data/details.xlsx");
            FileInputStream fis= new FileInputStream(file);
            XSSFWorkbook wb=new XSSFWorkbook(fis);
            System.out.println("Enter the sheet you want to search in: ");
            Scanner sc= new Scanner(System.in);
            int n= sc.nextInt(); 
            XSSFSheet sheet=wb.getSheetAt(n-1);
            Iterator<Row> itr=sheet.iterator();
            
            
            while(itr.hasNext()) {
                
                
                
                
                Row row=itr.next();
                
                Iterator<Cell> cellIterator=row.cellIterator();
                while(cellIterator.hasNext()) {
                    Cell cell=cellIterator.next();
                    Cell name_cell=row.getCell(0);
                    String name=name_cell.getStringCellValue();
                    if (cell.getRowIndex()!=0 && cell.getStringCellValue().equals("")&& cell.getColumnIndex()!=0){
                        int idate=cell.getColumnIndex();
                        Row first_row=sheet.getRow(0); 
                        Cell date_cell=first_row.getCell(idate);
                        
                        Date sdate=date_cell.getDateCellValue();
                        
                        
                      
                            
                        if (ExcelReader.DayCheck(sdate)){
                            System.out.println("No entry found for "+name+" on "+sdate);
                            
                            
                        }
                        
                        
                        
                            
                    }
                    
                    
                    
                    
                    }
                
                }
                System.out.println("");
            }
            
        catch(Exception e)  
        {  
        e.printStackTrace();  
        }  
        }
    
    public static boolean DayCheck(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int day = cal.get(Calendar.DAY_OF_WEEK);
        return (day != Calendar.SATURDAY && day != Calendar.SUNDAY);
        
    }

}

这个问题不像其他问题,我只需要将日期格式更改为 dd-mm-yyyy 我需要获取包含日日期月时间和区域设置信息的日期以dd-mm-格式提取日期年月日

我引入了这两个函数来创建我需要的日期格式。


        public static String getDayStringOld(Date date, Locale locale){

        DateFormat formatter = new SimpleDateFormat("EEEE", locale);
        return formatter.format(date);}
    
    public static String getmonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month= cal.get(Calendar.MONTH);
        
        String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        return monthNames[month];
        
    }
}

另外,我更改了 output 打印语句以按照我想要的方式打印日期。

        System.out.println("No entry found for "+name+" on "+ strDate.substring(8,10)+"-"+getmonth( sdate)+"-"+strDate.substring(24,28) +" "+ getDayStringOld(sdate,locale));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM