user496949
user496949

Reputation: 86135

Is the encoding name UTF8 or UTF-8?

Some httpclient requires me to give a string charsetµ.

I am wondering whether to give it UTF8 or utf8 or UTF-8?

Especially when calling Charset.forName(enc)

Upvotes: 39

Views: 33086

Answers (5)

Yaser
Yaser

Reputation: 98

Charset charset = Charset.forName("cp1254"); 

This was the utf-8 solution for me. You can use it.

Upvotes: 0

Ridox
Ridox

Reputation: 1389

The standard name is 'UTF-8'. Source code(I use jdk1.8.0_20) showes more detail:

UTF_8 extends Unicode {
    public UTF_8() {
        super("UTF-8", StandardCharsets.aliases_UTF_8); // show other aliases
        // static final String[] aliases_UTF_8 = new String[]{"UTF8", "unicode-1-1-utf-8"};
    }

    public String historicalName() {
        // return old name of UTF-8
        return "UTF8";
    }

Upvotes: 0

ioreskovic
ioreskovic

Reputation: 5699

UTF-8 is the standard one and most people use it.

Here is the list of charsets used in Java:

http://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html

N.B.
If charset factory/singleton classes can take Enum as parameter, as well as String, Enum is, IMO, always a safer and better option.

Upvotes: 22

Bob Hastings
Bob Hastings

Reputation: 16

FYI, For Java 7/tomcat 8.5 I am using: -Dfile.encoding=UTF-8 -Dclient.encoding.override=UTF-8

But for Java 8/tomcat 8.5 I got: java.nio.charset.IllegalCharsetNameException: UTF-8

So I changed to UTF8

Upvotes: 0

Julien Nioche
Julien Nioche

Reputation: 4864

Easier to use StandardCharsets.UTF_8 as it saves you having to deal with UnsupportedEncodingExceptions

Upvotes: 29

Related Questions