0

I want to date format change with java code.

Input value will be "2015/07/02".

Output value want to be "02-JUL-15".

I want to know date format of "02-JUL-15". How to change the date format? Please help!

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Zay Ya
  • 195
  • 1
  • 4
  • 15
  • I'm assuming the input value is a `String`? FYI: `Date` doesn't have an internal format of it's, it's just a container for the number of milliseconds since the epoch – MadProgrammer Jul 24 '15 at 02:46
  • Maybe something like [this](http://stackoverflow.com/questions/10787281/converting-string-format-date-for-date-object) or [this](http://stackoverflow.com/questions/20622548/change-date-and-time-format)? – MadProgrammer Jul 24 '15 at 02:49
  • Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to *specific* programming problems -- but we will happily help you write it yourself! Tell us [what you've tried](http://whathaveyoutried.com), and where you are stuck. This will also help us answer your question better. – Elliott Frisch Jul 24 '15 at 03:04
  • Goodness. Have you even bothered to look at the Javadocs for the Java Date API classes? It would have taken you less time to find the answers yourself than it took to write this question. – scottb Jul 24 '15 at 05:15
  • @ Elliott Frisch ⇒ Sorry for my bad manners. Actually, I want to know date format of "02-JUL-15". – Zay Ya Jul 28 '15 at 06:24

3 Answers3

0

Simplest way to do this is to use SimpleDateFormat in java,

DateFormat date = new SimpleDateFormat("dd-MMM-yy");
date.format(yourdate); //yourdate in this case is "2015/07/02" wherever that is stored

To answer you question, the format of "02-JUL-15" is dd-MMM-yy, Try not to use java.util.date most of its methods have been deprecated

0
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * This Example
 * Takes an input in the format YYYY/MM/DD i.e 2015/07/24
 * and converts back in the format DD-MON-YY ie 24-JUL-15
 */
public class DateConverter1 {

    public static void main(String...strings) throws ParseException {

        // define the date format in which you want to take the input
        // i.e YYYY/MM/DD correspond to yyyy/MM/dd

        DateFormat inputDateFormat = new SimpleDateFormat("yyyy/MM/dd");

        // convert your date into desired input date format
        Date inputDate = inputDateFormat.parse("2015/07/20");
        // above lines gives Parse Exception if date is UnParsable


        // define the date format in which you want to give output
        // i.e dd-MMM-yy

        DateFormat outputDateFormat = new SimpleDateFormat("dd-MMM-yy");

        // don't assign it back to date as format return back String
        String outputDate = outputDateFormat.format(inputDate);

        System.out.println(outputDate);



    }
}

Output:

20-Jul-15
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44
0

@Ankur Anand And @ Manish Mallavarapu ⇒ now, I found solution about "dd-MMM-yy". when we set up format "English (United States)" at region and language within my pc, we found output "20-Jul-15". But, when we set up format "japanese" at region and language, we found output "20-07-15".

Zay Ya
  • 195
  • 1
  • 4
  • 15