0

Currently, i'm trying to format my date into a specific format:

here is my method that returns a date:

 public Date createDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return sdf.format(cal. getTime());
    }

my format:

  private String myFormat = "MM/dd/yyyy"; //In which you need put here
    private SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

now the problem is when i try to use sdf.format(cal.getTime());

it says that it is incompatible type because it found a string when i try to use the sdf.format.

would it be possible to use the format the date into the format i want without converting it to string?

  • Your `createDate` need to be String type.. sdf.format will return a String. – Ashwini Saini Oct 01 '18 at 03:25
  • You mean without converting it to a `Date`? Because it'll be a string after formatting. – Henry Oct 01 '18 at 03:25
  • That's meant to be a string... A format always a string... Well if you want to get date then you can use sdf.parse() but this will return a date same as you had previously.. – Ashwini Saini Oct 01 '18 at 03:28
  • @AshwiniViolet yes. sdf.parse is new to me. i'll look it up and i might find my own answer. thanks for this. –  Oct 01 '18 at 03:41
  • @Henry, hi , i need to format my date without converting it to string, would that possible? –  Oct 01 '18 at 03:44
  • That does not make sense. Only the string representation of a `Date` has a format. The `Date` itself is basically just the number of milliseconds since Jan 1st, 1970. – Henry Oct 01 '18 at 04:05
  • @Henry oh i get it. i currently getting date like this "Wed Oct 03 00:00:00 GMT+08:00 2018" and i want to convert it into "10/03/18" without making it as a string. –  Oct 01 '18 at 04:21
  • as Henry says, Date itself is a `Long` number of milliseconds since start of 1970 year. Date can't be formatted itself. Only it's `String` representation can be formatted. So it does not make sense to format Date without converting it to String. If you still think that you need to do that, you may want to explain why, and what you want to do with that formatted date – Vladyslav Matviienko Oct 01 '18 at 05:02
  • 1
    Possible duplicate of [want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format](https://stackoverflow.com/questions/8745297/want-current-date-and-time-in-dd-mm-yyyy-hhmmss-ss-format) – Ole V.V. Oct 01 '18 at 08:17
  • And/or possible duplicate of [return date type with format in java](https://stackoverflow.com/questions/50485203/return-date-type-with-format-in-java) and/or [how to change the format of Calendar object](https://stackoverflow.com/questions/50286811/how-to-change-the-format-of-calendar-object). – Ole V.V. Oct 01 '18 at 08:22
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Oct 02 '18 at 12:33

2 Answers2

0

Yes you can do this for example :

DateFormat dateFormat = new SimpleDateFormat(currFormat);
    Date date = null;
    // date conversion
    try {
        date = dateFormat.parse(dateTimeStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    DateFormat outputFormat = new SimpleDateFormat(requiredFormat);
    return outputFormat.format(date);

you have to convert your string date into Date format first then you will format it.

unzila
  • 190
  • 1
  • 12
  • incompatible type for DateFormat finalDateFormat = new SimpleDateFormat("dd-MMM-yyyy"); –  Oct 01 '18 at 05:00
  • its not incompatible you can use this too : SimpleDateFormat finalDateFormat = new SimpleDateFormat("dd-MMM-yyyy"); – unzila Oct 01 '18 at 05:11
  • what format is : "Thu Nov 01 00:00:00 GMT+08:00 2018" ? –  Oct 01 '18 at 05:29
  • in that case, what will i put in the currFormat? –  Oct 01 '18 at 05:39
  • curr format is : EEE,MMM dd HH:mm yyyy required format is : "MM/dd/yyyy" – unzila Oct 01 '18 at 05:44
  • The return outputFormat.format(date) will still return a string, not a date. –  Oct 01 '18 at 06:20
  • for this you just again parse output string date into Date like this : date = dateFormat.parse(outputdate); – unzila Oct 01 '18 at 06:31
0
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy"); 
    Date c = Calendar.getInstance().getTime();
    dateFormat.format(c);

This snippet you can use to get your answer.

  • incompatible types for : DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy"); –  Oct 01 '18 at 05:00
  • I have edited answer try that.dateFormat will be in String format.You can access that and can convert again to date format. – hemant kamat Oct 01 '18 at 05:18