NET PACKAGE / CATATAN</>↗Net Package11 Agt 2025
Here are the most common and reliable ways to get hostnames and IP addresses in Java (Java 21). Pick the approach that matches your runtime (desktop app, server app, behind proxy, etc.). Quick local host info Good for simple cases, but can return 127.0.0.1 if your host isn’t configured in DNS/hosts. import java.net.InetAddress; public class […]
NET PACKAGE / CATATAN</>↗Net Package09 Agt 2025
Building a simple web server in Java involves creating a server socket to listen on a specific port, accepting client requests, and sending responses back to the client. Below is a basic example of building a simple HTTP server in Java. Example Code package org.kodejava.net; import java.io.*; import java.net.*; public class SimpleWebServer { public static […]
JAVA DATE TIME API / CATATAN</>↗Java Date Time API08 Agt 2025
Here are simple and reliable ways to check whether a date falls on a weekend in Java. Prefer the modern java.time API (Java 8+), which is clearer and thread-safe. Using LocalDate (recommended) import java.time.DayOfWeek; import java.time.LocalDate; public class WeekendChecker { public static boolean isWeekend(LocalDate date) { DayOfWeek dow = date.getDayOfWeek(); return dow == DayOfWeek.SATURDAY || […]
JAVA DATE TIME API / CATATAN</>↗Java Date Time API07 Agt 2025
In Java, you can convert between java.util.Date and java.time.LocalDateTime using the java.time API introduced in Java 8. Here’s how you can perform the conversions: 1. Converting Date to LocalDateTime You need to use java.time.Instant and java.time.ZoneId to make this conversion. Here’s the process: package org.kodejava.datetime; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class DateToLocalDateTimeExample { […]
JAVA DATE TIME API / CATATAN</>↗Java Date Time API06 Agt 2025
You can calculate date differences in Java using the ChronoUnit enum from the java.time package. The ChronoUnit class is used to measure the amount of time between two temporal objects (e.g., LocalDate, LocalDateTime, etc.) in terms of specific time units like DAYS, MONTHS, YEARS, etc. Here’s an example of how to calculate the difference between […]
IO / CATATAN</>↗IO05 Agt 2025
To read a binary file into a byte array in Java, you can use various ways such as Files.readAllBytes(), FileInputStream, or DataInputStream. Below is an explanation of the most common methods. Using Files.readAllBytes() (Java NIO) This is the simplest and most efficient way if you’re using Java 7 or later. The Files.readAllBytes() method reads all […]