1

I am a newbie to programming, my teacher has assigned me a homework, which one part of it is to show the date that entered. I used SimpleDateFormat "dd/MMMM/yyyy". But when is running, It shows the month in the language of my country.

For exam, if I enter 10-02-2001, is will show up 10-tháng 2( february in my country)-2001. I want change it to English. Please help me. (Sorry for my bad English)

The code:

public Date getDate(String msg, String err, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        sdf.setLenient(false);
        while(true){
            try {
                System.out.print(msg);
                String value = input.readLine();
                date = sdf.parse(value);
                Date now = new Date();
                if(date.after(now)){
                    System.out.println("Your input date is bigger than present");
                    continue;
                }
                break;
                
            } catch (IOException | ParseException ex) {
                System.out.println(err);
            }
        }
        return date;
    } 

Date dateInput = validate.getDate("Enter Date: ", "Date invalid", "dd/MM/yyyy");
        
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
        String date = sdf.format(dateInput);
        
        double amount = validate.getDouble("Enter Amount: ", "Please enter a real number", 0, Double.MAX_VALUE);
            
Xjodia
  • 13
  • 2
  • I think you can utilize a `Locale` for this instead of implicitly using the one of your system. Have a look at [this post](https://stackoverflow.com/questions/1661325/simpledateformat-and-locale-based-format-string/1661389). But please consider using a modern api for date and time, that's `java.time`. – deHaar Feb 24 '21 at 06:50
  • Does this answer your question? [SimpleDateFormat returns string date in different languages](https://stackoverflow.com/questions/17421660/simpledateformat-returns-string-date-in-different-languages) – matt freake Feb 24 '21 at 06:53
  • 1
    You can use java localization features, in addtion you should consider to learn to use the new Java DateTime API introduced in java8, https://stackoverflow.com/questions/1661325/simpledateformat-and-locale-based-format-string – eHayik Feb 24 '21 at 06:53
  • use locale with Simgple Date format. E.g: SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); – akshaya pandey Feb 24 '21 at 06:54

2 Answers2

2

You have to specify the locale as well, like so:

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);

If you do not specify the locale it will use the default locale. See the javadoc for more information.

geco17
  • 5,152
  • 3
  • 21
  • 38
0

You can pass locale to SimpleDateFormat costructor. Try this:

        Date now = new Date();
        System.out.println(new SimpleDateFormat​("dd/MMMM/yyyy", Locale.CHINA).format(now));    
        System.out.println(new SimpleDateFormat​("dd/MMMM/yyyy", Locale.US).format(now));

And as of java 8 you can do somthing like that:

    System.out.println(Instant.now()
            .atZone(ZoneId.systemDefault())
            .format(DateTimeFormatter.ISO_DATE));
S. Kadakov
  • 861
  • 1
  • 6
  • 15