0

I have the date format as "dd/MM/yyyy", i would like to convert this into MM DD, yyy an example:

10/10/1996 -> Oct 10, 1996

Also the other way around case:

  Oct 10, 1996 -->   10/10/1996

I have been trying for 1+ hour but couldnt figure it out as i have never used a date formatter class, if anyone could help me it would be awesome, PL: java

FlyingNades
  • 432
  • 3
  • 16
cs guy
  • 926
  • 2
  • 13
  • 33
  • Please review the duplicate link. If you actually try something and get stuck, then edit your question. – Tim Biegeleisen Apr 07 '18 at 10:29
  • For most purposes, rather than converting your strings back and forth between date formats you should keep your date in a `LocalDate` variable and only format it into one or the other string format for display or serialization, the latter typically for transmission or storage. For not-brand-new Android add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to be able to use `org.threeten.bp.LocalDate` and the other classes from `java.time`, the modern Java date and time API. – Ole V.V. Apr 07 '18 at 12:47
  • [Oracle Tutorial: Date Time, Parsing and Formatting](https://docs.oracle.com/javase/tutorial/datetime/iso/format.html) (that’s a link). – Ole V.V. Apr 07 '18 at 13:44

2 Answers2

1

Im assuming your'e using DateTime.

SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
String strDate = sdfDate.format(now);

This code will format a DateTime to string as 2018-04-07. You can play around with the format as you want. search for list of the format keys (such as MM, as I mentioned here).

FlyingNades
  • 432
  • 3
  • 16
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class as the first option and 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`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Apr 07 '18 at 10:47
  • @OleV.V. Agreed. – FlyingNades Apr 07 '18 at 10:48
0

//Use these imports

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

Use this code to convert your date format from one to another:

         String fromDateFormat = "dd/MM/yyyy";
         String fromdate = 15/03/2018; //Take any date

         String CheckFormat = "dd MMM yyyy";//take another format like dd/MMM/yyyy
         String dateStringFrom;

         Date DF = new Date();


          try
          {
             //DateFormatdf = DateFormat.getDateInstance(DateFormat.SHORT);
             DateFormat FromDF = new SimpleDateFormat(fromDateFormat);
             FromDF.setLenient(false);  // this is important!
             Date FromDate = FromDF.parse(fromdate);
             dateStringFrom = new 
             SimpleDateFormat(CheckFormat).format(FromDate);
             DateFormat FromDF1 = new SimpleDateFormat(CheckFormat);
             DF=FromDF1.parse(dateStringFrom);
             System.out.println(dateStringFrom);
          }
          catch(Exception ex)
          {

              System.out.println("Date error");

          }

Note: Change your SimpleDateFormat accordingly.

Däñish Shärmà
  • 2,891
  • 2
  • 25
  • 43
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class as the first option and 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`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Apr 07 '18 at 10:47