-1

I have two strings for a date (using the current year):

  1. a day = "15"
  2. a month = "7"

With this data I would like to have a string date with this format: Tuesday July 15

Is that possible?

I´m trying with this code but it doesn't work:

 public void dateFormatMoth(String day, String month){

 Date date = null;

    String newDateStr="";
    String dateReceived="day/month";

    SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM");
    SimpleDateFormat myFormat = new SimpleDateFormat("EEEE-MM-dd");

    try {

         reformattedStr = myFormat.format(fromUser.parse(dateReceived));
    } catch (ParseException e) {
        e.printStackTrace();
    }

   Log.d(LOG_TAG,"day formatted"+newDateStr);
}
Sam R.
  • 16,027
  • 12
  • 69
  • 122
JoCuTo
  • 2,463
  • 4
  • 28
  • 44

3 Answers3

1

Just change your myFormat slightly to achieve this. MM will give you month as number, MMMM will give you the full name:

public void dateFormatMonth(String day, String month) throws ParseException {
    SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM");
    SimpleDateFormat myFormat = new SimpleDateFormat("EEEE MMMM dd", Locale.US);
    try {
        reformattedStr = myFormat.format(fromUser.parse(day + "/" + month));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Log.d(LOG_TAG,"day formatted"+newDateStr);
}

You can read more about the different formats in the JavaDoc.

Note that I specified the locale explicitly, to make sure you got the output you requested regardless of the locale of your machine.

Keppil
  • 45,603
  • 8
  • 97
  • 119
1

java.time.MonthDay#of(int month, int dayOfMonth)

You can use MonthDay#of(int month, int dayOfMonth) to create an instance of MonthDay out of the given month and the day of the month.

You need a year

Every year 15-Jul may fall on a different day of the week e.g. in 2014, it falls on Tuesday and in the current year 2023, it falls on Saturday. So, you need a year to get the day of the week out of the MonthDay.

By assigning a year to the MonthDay, you get a LocalDate which can give you the day of the week and many other pieces of information e.g. week number, whether it is before/after another day etc.

Demo:

import java.time.LocalDate;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(weekdayMonthDayFormatted("15", "7"));
    }

    static String weekdayMonthDayFormatted(String day, String month) {
        MonthDay md = MonthDay.of(Integer.parseInt(month), Integer.parseInt(day));

        // I have used 2014 here just for the demo. Replace 2014 with the applicable
        // year e.g. for the current year, replace it with Year.now().getValue()
        LocalDate date = md.atYear(2014);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE MMMM dd", Locale.ENGLISH);
        return date.format(formatter);
    }
}

Output:

Tuesday July 15

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Note: In Mar 2014, the java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern date-time API. Since then, it is highly recommended to stop using the legacy date-time API.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Try:

    public static String format(String input_date, String input_format, String output_format) {

        SimpleDateFormat simple_date_input_format;
        SimpleDateFormat simple_date_output_format;
        simple_date_input_format = new SimpleDateFormat(input_format, Locale.US);
        simple_date_output_format = new SimpleDateFormat(output_format, Locale.US);

        try {
            Date input_date_formatted = simple_date_input_format.parse(input_date);
            output_date_formatted = simple_date_output_format.format(input_date_formatted);
        } catch (ParseException e) {
            output_date_formatted = null;
            e.printStackTrace();
        }
        return output_date_formatted;
moatist
  • 198
  • 1
  • 13