简体   繁体   English

从 Excel 更改日期格式

[英]Change format of date from Excel

I have written a Java file Handling program which fetches the date and days from an excel file where no entry has been made.我写了一个 Java 文件处理程序,它从一个没有输入的 excel 文件中获取日期和日期。

The excel file stores the attendance records for some people. excel 文件存储了一些人的考勤记录。 and if there has been no entry for a particular day then the program should check if the day was a weekend.如果某一天没有条目,那么程序应该检查这一天是否是周末。 If it is not a weekend then the porgram should output the name and the day and date.如果不是周末,则程序应包含姓名、日期和日期 output。

However the current format of the output is not as I want it to be:但是 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

I want the output date to be in the style 01 August 2022, Monday我希望 output 日期的样式为 2022 年 8 月 1 日,星期一

How do I achieve that?我该如何实现?

Following is the code以下是代码


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);
        
    }

}

This question is not like the other questions where I simply need to change the format of date to dd-mm-yyyy I need to take the date containing the day date month time and locale info to extract the date in the format dd-mm-yyyy, Day这个问题不像其他问题,我只需要将日期格式更改为 dd-mm-yyyy 我需要获取包含日日期月时间和区域设置信息的日期以dd-mm-格式提取日期年月日

I introduced these two functions to create the date format I needed.我引入了这两个函数来创建我需要的日期格式。


        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];
        
    }
}

Also, I changed the output print statemnt to print the date in the way I wanted.另外,我更改了 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