3

I am having confusion on understanding the following piece of code:


public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String parsingDate = "२०२४-११-३० १२:५१:४६";
        try {
            Date date = sdf.parse(parsingDate);
            System.out.println("Date " + date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    

The value stored in parsingDate "२०२४-११-३० १२:५१:४६" is Nepali language equivalent of "2024-11-30 12:51:46".

I assumed that the above piece of code would throw an exception, but to my surprise, the code works perfectly. The output of the program is Sat Nov 30 12:51:46 NPT 2024

How does it work?

16
  • Is your jvm's default locale set to Nepal? Or user.language is set to Nepali? Commented Dec 1, 2024 at 6:51
  • 3
    Note that since Java 8, you should use date-time API. When parsing dates, you can explicitly set the locale. Commented Dec 1, 2024 at 7:38
  • 1
    I suspect this works for any Locale and any Unicode digits - most probably it is using the numeric value of the unicode characters (e.g. Character.getNumericValue('२') == 2) Commented Dec 1, 2024 at 8:43
  • 1
    @Basil -- withLocale() != localizedBy() (also explained in previous comment) - to set the decimal style, localizedBy() should be used (as documented) Commented Dec 3, 2024 at 21:56
  • 1
    ( and DecimalStyle.getZeroDigit() is used to convert digits to number [ just subtracted from the char { no code point <yet?> } ] ) Commented Dec 3, 2024 at 22:07

3 Answers 3

2

Not an 'answer' as such but worthwhile to show the correct way of dealing with your non-ISO-compliant datetime strings, avoiding the use of obsolescent classes. Thanks to user85421 for the correct way to deal with localisation for the Devangari digits:

import static java.time.format.DateTimeFormatter.ISO_DATE;
import static java.time.format.DateTimeFormatter.ISO_TIME;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class NepalTimeParser {
    public static void main(String[] args) throws Exception {
        DateTimeFormatter dtfNepali = new DateTimeFormatterBuilder().append(ISO_DATE).appendLiteral(" ")
                .append(ISO_TIME).toFormatter().localizedBy(Locale.of("ne", "NP"));
        String parsingDate = "२०२४-११-३० १२:५१:४६";
        if (args.length > 0) {
            parsingDate = args[0];
        }
        LocalDateTime ldt = LocalDateTime.parse(parsingDate, dtfNepali);
        System.out.println(ldt);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I really do appreciate the effort you and @user85421 have put into this
1

If I got the question right, all answers so far (first one, second one) do not come close to the desired answer.

My understanding is, that the thread opener was surprised that the method SimpleDateFormat::parse is able to handle non-arabic digits.

Which number signs (characters for digits) are valid digits is not related to the current Locale selected – the TO confirmed that they did not use the Nepali Locale, one comment says that the result was the same for the German Locale. It is just a definition made by Unicode which character is a numerical character.

And obviously does the parse() of java.text.SimpleDateFormat translate each valid numerical character properly to its numerical value before it tries to parse the String as a date/time value. That's a nice feature to have …

That the methods and APIs dealing with java.util.Date are long time outdated is true, but completely unrelated to the question. Not to mention that the trick does not work with java.time.format.DateTimeFormatter.

1 Comment

I think you got the question right. However it seems that the asker got wrong whether to use SimpleDateFormat at all. It’s obsolescent, and it was a notorious trouble maker back when it was used. So maybe put bluntly, your answer is correct and irrelevant.
0

In simple terms, SimpleDateFormat knows a Locale, and unless you explicitly set it to something different, it will be your system's default Locale. It uses that Locale when parsing or formatting dates, which is what you're seeing here.

Of course SimpleDateFormat has been obsolete for almost 12 years now, and you should be using the java.time equivalent class, which is DateTimeFormatter. This class also knows a Locale, so it should act the same way (although I've never tried it in Nepal).

1 Comment

( almost 11 years - Java 8 was released March 2014... we are still in 2024 :- ) ) and, again, SimpleDateFormat does not use the (default) Locale for parsing numbers (ideone.com/ezi3x5)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.