Java date conversion formats [closed]

情到浓时终转凉″ 提交于 2019-12-13 09:04:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!