问题
from date format change to this format - 01-Jan-2019 in excel using java. I want to read the date format in excel. if suppose having any date format want to change DD/MMM/YYYY format. ie., 01-Jan-2019. any one please suggest and help me.
回答1:
I could not get your requirement clearly. However, if you are looking for a way to convert a date into various formats, given below is an example:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateFormatsDemo {
public static void main(String[] args) {
//Convert date to date string in the format MM/dd/yyyy
DateTimeFormatter farmatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate localDate = LocalDate.now();
String dateString = farmatter.format(localDate);
System.out.println(dateString);
//Convert date string in one format (MM/dd/yyyy) into date string in another format (MM-dd-yyyy)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
localDate= LocalDate.parse(dateString, formatter);
farmatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
dateString = farmatter.format(localDate);
System.out.println(dateString);
}
}
If you are looking to work with MS-Excel using Java, you can use Apache POI.
回答2:
Use the SimpleDateFormat to format your date:
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
System.out.println(formatter.format(date));
Or have a look here: Baledung - A Guide to SimpleDateFormat
来源:https://stackoverflow.com/questions/59099470/java-date-conversion-formats