How does Base64 encoding work?

Base64 encoding scheme is designed to encode binary data, especially when that data needs to be stored and transferred over media that designed to deal with text. Base64 encoding helps to ensure that the data remains intact without modification during transport.

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. Base64 is used commonly in a number of applications including email via MIME (Multipurpose Internet Mail Extensions), and storing complex data in XML or JSON.

Radix-64 refers to a numeral system that uses 64 unique characters to represent data. In the case of Base64 encoding, the 64 characters are typically the uppercase letters AZ, the lowercase letters az, the numerals 09, and an additional two characters, usually + and /. These characters are used to encode data into a text format that can be safely transferred over various systems designed to handle text data.

Here’s a brief overview of how the Base64 encoding process works:

  • Take the input data (which is binary data). For example, we have the text “Hello”. Each character is converted into their ASCII code.
  • Split the input data into chunks of 24 bits (3 bytes). If the input is not divisible by 24, it is padded with zeros on the right to make up a full chunk.
ASCII H = 72 e = 101 l = 108 l = 108 o = 111
Binary 01001000 01100101 01101100 01101100 01101111
  • Each chunk of 24 bits is then split into four chunks of 6 bits.
6-bits 010010 000110 010101 101100
Value 18 6 21 44
6-bits 011011 000110 111100 [00 = PAD]
Value 27 6 60
  • Each 6-bit chunk is then mapped to an encoded character using an index table (below), which is a set of 64 distinct characters—these are A–Z, a–z, 0–9, + and / in the Base64 alphabet.
Value 18 6 21 44 27 6 60
Encoding S G V s b G 8
  • If the last 8-bit block (third block for normal Base64 encoding) had the padding zero bits, the output of the corresponding 6-bit block (fourth block for normal Base64 encoding) is replaced with one = symbol. If the second 8-bit block also had padding zeros, then fourth, and third blocks of Base64 encoding will have = symbols.
Value 18 6 21 44 27 6 60 PAD
Encoding S G V s b G 8 =
  • The output is the encoded string.
SGVsbG8=

This process can be reversed to decode a Base64 string back into the original binary data.

Do note, though, while Base64 can encode any binary data, it is not encryption nor should it be used for encryption purposes – it does not hide or secure information, it’s merely an encoding scheme.

Base64 Index Table

Value Encoding Value Encoding Value Encoding Value Encoding
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /

Differences between getEncoder() and getUrlEncoder() method of java.util.Base64 class

The java.util.Base64.getEncoder() and java.util.Base64.getUrlEncoder() methods in Java both return a Base64 encoder. However, they exhibit different behaviors mainly due to the encoding scheme they follow for handling URL and filename safe characters.

  • java.util.Base64.getEncoder(): This method returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme as per RFC 4648 Section 4.

Basic Base64 encoding does not handle URL and filename safe characters well. It uses + for 62, / for 63, and = for padding. These characters can cause problems in URLs.

An example of its usage.

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64EncoderExample {
    public static void main(String[] args) {
        Base64.Encoder encoder = Base64.getEncoder();
        String str = "https://www.google.com/search?q=Hello+World";
        String encodedString = encoder.encodeToString(str.getBytes(StandardCharsets.UTF_8));
        System.out.println("encodedString = " + encodedString);
    }
}

Output:

encodedString = aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g/cT1IZWxsbytXb3JsZA==
  • java.util.Base64.getUrlEncoder(): This method returns a Base64.Encoder that encodes in URL and filename safe type base64 scheme as per RFC 4648 Section 5.

URL and filename safe Base64 encoding replaces + with - , and / with _. It also omits padding characters. This makes it safer for use in URLs or file-system paths.

An example of its usage.

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64UrlEncoderExample {
    public static void main(String[] args) {
        Base64.Encoder urlEncoder = Base64.getUrlEncoder();
        String str = "https://www.google.com/search?q=Hello+World";
        String urlEncodedString = urlEncoder.encodeToString(str.getBytes(StandardCharsets.UTF_8));
        System.out.println("encodedString = " + urlEncodedString);
    }
}

Output:

encodedString = aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g_cT1IZWxsbytXb3JsZA==

So the choice between these two would depend on where you intend to use the encoded bytes. If it’s to be in a URL or a filepath, it’s recommended to use the getUrlEncoder() due to its url safe encoding scheme. For other use cases, the getEncoder() method should suffice.

Similar to the getEncoder() and getUrlEncoder() methods, the java.util.Base64 class provides the getDecoder() and getUrlDecoder() methods for Base64 decoding.

  • java.util.Base64.getDecoder(): This method returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme.

Example:

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64DecodeExample {
    public static void main(String[] args) {
        Base64.Decoder decoder = Base64.getDecoder();

        // "Hello, World!" in Base64
        String str = "SGVsbG8sIFdvcmxkIQ==";
        String decodedString = new String(decoder.decode(str), StandardCharsets.UTF_8);
        System.out.println("decodedString = " + decodedString);
    }
}

Output:

decodedString = Hello, World!
  • java.util.Base64.getUrlDecoder(): This method returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme.

Example:

package org.kodejava.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64UrlDecoderExample {
    public static void main(String[] args) {
        Base64.Decoder urlDecoder = Base64.getUrlDecoder();

        // "Hello, World!" in URL-safe Base64
        String urlSafeStr = "SGVsbG8sIFdvcmxkIQ";
        String decodedString = new String(urlDecoder.decode(urlSafeStr), StandardCharsets.UTF_8);
        System.out.println("decodedString = " + decodedString);
    }
}

Output:

decodedString = Hello, World!

The choice between these two would again depend on from where you are getting the encoded bytes. If it’s from a URL or a filepath, you would want to use getUrlDecoder(). For other use cases, the getDecoder() method should suffice.

How do I convert LocalDate to ZonedDateTime?

You can use the atStartOfDay() method from LocalDate class to convert a LocalDate into a LocalDateTime. Then, you need to convert LocalDateTime to a ZonedDateTime using the atZone() method.

Here is an example:

package org.kodejava.datetime;

import java.time.*;

public class LocalDateToZonedDateTimeExample {
    public static void main(String[] args) {
        // Create a LocalDate
        LocalDate date = LocalDate.of(2023, Month.JULY, 9);
        System.out.println("LocalDate: " + date);

        // Convert LocalDate to LocalDateTime
        LocalDateTime dateTime = date.atStartOfDay();
        System.out.println("LocalDateTime: " + dateTime);

        // Convert LocalDateTime to ZonedDateTime
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}

Output:

LocalDate: 2023-07-09
LocalDateTime: 2023-07-09T00:00
ZonedDateTime: 2023-07-09T00:00+08:00[Asia/Makassar]

In this example, we’re creating a LocalDate for July 9, 2023. Then we’re converting it to a LocalDateTime, and then to a ZonedDateTime. The atStartOfDay() method returns a LocalDateTime set to the start of the day (00:00) on the date of this LocalDate. The atZone() method then takes the ZoneId and returns a ZonedDateTime representing the start of the day in that timezone.

The ZoneId.systemDefault() returns the system default time zone. If you want to convert it to a specific time zone, you can specify the timezone as a string, like this: ZoneId.of("America/New_York").

How do I convert datetime between time zones?

The ZonedDateTime class is part of the Java Date-Time Package (java.time.*), released in Java 8 to address the shortcomings of the old date-time classes such as java.util.Date, java.util.Calendar, and java.util.SimpleDateFormat.

Some of the key features are:

  • It represents a date-time with a timezone in the ISO-8601 calendar system, such as ‘2007-12-03T10:15:30+01:00 Europe/Paris’.
  • It provides a lot of methods to play with year, month, day, hour, minute, second and nanosecond fields of the datetime.
  • It’s an immutable class, which is good for multithreaded environments.
  • It provides a fluent interface, which allows method calls to be chained.

Using Java java.time package (which is part of Java 8 and later), you can convert dates between time zones like this:

package org.kodejava.datetime;

import java.time.*;

public class ZonedDateTimeExample {
    public static void main(String[] args) {

        // Create a ZonedDateTime instance for the current date/time
        // in the current timezone
        ZonedDateTime now = ZonedDateTime.now();

        // Create a ZonedDateTime instance for the current date/time
        // in a different timezone
        ZonedDateTime nowInJakarta = now.withZoneSameInstant(ZoneId.of("Asia/Jakarta"));

        // Print the current date/time in the current timezone
        System.out.println("Current date/time: " + now);

        // Print the current date/time in the different timezone
        System.out.println("Current date/time in Jakarta: " + nowInJakarta);
    }
}

Output:

Current date/time: 2024-01-20T21:33:31.236022700+08:00[Asia/Makassar]
Current date/time in Jakarta: 2024-01-20T20:33:31.236022700+07:00[Asia/Jakarta]

The withZoneSameInstant method is used to adjust the date and time based on the timezone. It can be used to convert a datetime value to the datetime in another timezone.

This program will create a ZonedDateTime object representing the current date and time, and then create another ZonedDateTime object that represents the current date and time in Jakarta. Finally, it will print both dates to the console.

What is ZoneRules class of Java Date-Time API?

The ZoneRules class in Java’s Date-Time API is used to encapsulate the set of rules defining how the zone offset varies for a single time zone.

The information in this class is typically derived from the IANA Time Zone Database (TZDB). The rules model the data traditionally contained in the ‘zic’ compiled data files of information from the TZDB.

An instance of ZoneRules is obtained from a ZoneId using the ZoneId.getRules() method.

Here is a simple example of how to use the ZoneRules class:

package org.kodejava.datetime;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.zone.ZoneRules;

public class ZoneRulesExample {
    public static void main(String[] args) {
        // Get ZoneId for "Europe/Paris"
        ZoneId zoneId = ZoneId.of("Europe/Paris");
        System.out.println("ZoneId : " + zoneId);

        // Get ZoneRules associated with the ZoneId
        ZoneRules zoneRules = zoneId.getRules();
        System.out.println("ZoneRules : " + zoneRules);

        // Get the standard offset
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneOffset offset = zoneRules.getOffset(localDateTime);
        System.out.println("Offset for " + localDateTime + " is: " + offset);
    }
}

Here we are using LocalDateTime.now() to get the current time and the getOffset(LocalDateTime) method on ZoneRules to find the offset for that particular time. The API guarantees immutability and thread-safety of ZoneRules class.

This example will output:

  • The ZoneId which will be “Europe/Paris”
  • The ZoneRules for the “Europe/Paris” time zone
  • The ZoneOffset for the current LocalDateTime. This offset is the difference in time between the “Europe/Paris” time zone and UTC at the time provided.

Output:

ZoneId : Europe/Paris
ZoneRules : ZoneRules[currentStandardOffset=+01:00]
Offset for 2024-01-19T15:55:14.156977 is: +01:00