-2

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Gn gn
  • 1
  • 1
  • I do not understand your question. But if you are trying to format a Date object in Java, you need to use a DateFormatter, like SimpleDateFormat (https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) to format your date. – bbrinck Nov 29 '19 at 06:05
  • 1
    Can you maybe provide your code snippet ? – bbrinck Nov 29 '19 at 06:05
  • Welcome to Stack Overflow and sorry, your question isn’t clear to me either. A number of questions have been asked about reading dates from Excel and others about changing the format of date strings. I suggest you go searching and try something out. If you have trouble getting it to work, please show us the code you have and specify precisely in which way it is failing, that will make for a clearer question and will be likely to receive more and better answers. – Ole V.V. Nov 29 '19 at 21:10

2 Answers2

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-1

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

bbrinck
  • 953
  • 1
  • 9
  • 33
  • Or just look here: https://www.javatpoint.com/java-date-format – bbrinck Nov 29 '19 at 06:42
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Nov 29 '19 at 21:07