com.base4j.util.Base64Util Maven / Gradle / Ivy
The newest version!
package com.base4j.util;
import javax.xml.bind.DatatypeConverter;
import java.util.Calendar;
/**
* Base64字符转换
*/
public class Base64Util {
public static String encoder(String str) {
return DatatypeConverter.printBase64Binary(str.getBytes());
}
public static String decoder(String str) {
return new String(DatatypeConverter.parseBase64Binary(str));
}
public static String encoderHex(String str) {
return DatatypeConverter.printHexBinary(str.getBytes());
}
public static String decoderHex(String str) {
return new String(DatatypeConverter.parseHexBinary(str));
}
public static void main(String[] args) {
//Testing DatatypeConverter.printBase64Binary
String s1 = "Testing DatatypeConverter.printBase64Binary方法";
String encodeds1 = Base64Util.encoder(s1);
System.out.println(encodeds1);
String decodeds1 = Base64Util.decoder(encodeds1);
System.out.println(decodeds1);
//Testing DatatypeConverter.printHexBinary
String s2 = "Testing DatatypeConverter.printHexBinary沙拉酱实施22";
String encodeds2 = Base64Util.encoderHex(s2);
System.out.println(encodeds2);
String decodeds2 = Base64Util.decoderHex(encodeds2);
System.out.println(new String(decodeds2));
//Lexical representation of date time
System.out.println(DatatypeConverter.printDateTime(Calendar.getInstance()));
}
}