Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Working with StringBuilder, StringBuffer, Date, Calendar, and Wrapper Classes in Java

Tech 2

StringBuilder and StringBuffer Classes

These classes are used to creating mutable sequences of characters. StringBuilder is not thread-safe but offers better performance, while StringBuffer is thread-safe but slightly slower.

public class StringBuilderExample {
    public static void main(String[] args) {
        // Testing constructors
        StringBuilder builder1 = new StringBuilder();
        builder1.append("test");
        System.out.println("builder1: " + builder1);
        System.out.println("---");
        
        // Converting a String to StringBuilder
        StringBuilder builder2 = new StringBuilder("initial");
        System.out.println("builder2: " + builder2);
        System.out.println("---");
        
        // Appending multiple strings
        StringBuilder messageBuilder = new StringBuilder();
        messageBuilder.append("Hello");
        messageBuilder.append(" ");
        messageBuilder.append("Java");
        messageBuilder.append("!");
        System.out.println("messageBuilder: " + messageBuilder);
        System.out.println("---");
        
        // Converting back to String
        String finalMessage = messageBuilder.toString();
        System.out.println("String result: " + finalMessage);
    }
}

Date and Calendar Classes

The Date class repersents a specific instant in time, while Calendar proivdes methods to date manipulation and conversion.

Date Class Example

import java.util.Date;

public class DateExample {
    public static void main(String[] args) {
        // Default constructor uses current system time
        Date currentDate = new Date();
        System.out.println("Current date: " + currentDate);
        System.out.println("---");
        
        // Get milliseconds since epoch
        long milliseconds = currentDate.getTime();
        System.out.println("Milliseconds: " + milliseconds);
        System.out.println("---");
        
        // Create a specific date from milliseconds
        Date specificDate = new Date(1671978206541L);
        System.out.println("Specific date: " + specificDate);
    }
}

Calendar Class Example

import java.util.Calendar;

public class CalendarExample {
    public static void main(String[] args) {
        // Get calendar instance for current locale
        Calendar cal = Calendar.getInstance();
        System.out.println(cal);
        System.out.println("---");
        
        // Extract date components
        int currentYear = cal.get(Calendar.YEAR);
        int currentMonth = cal.get(Calendar.MONTH);
        int currentDay = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println(currentYear + "-" + (currentMonth + 1) + "-" + currentDay);
        System.out.println("---");
        
        // Set to a specific date
        cal.set(2023, Calendar.NOVEMBER, 15);
        int setYear = cal.get(Calendar.YEAR);
        int setMonth = cal.get(Calendar.MONTH);
        int setDay = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println(setYear + "-" + (setMonth + 1) + "-" + setDay);
    }
}

Primitive Wrapper Classes

Wrapper classes allow primitive types to be used as objects and provide utility methods.

public class WrapperExample {
    public static void main(String[] args) {
        int primitiveValue = 42;
        
        // Manual boxing (pre-JDK 5)
        Integer wrappedInt = new Integer(99);
        // Manual unboxing
        int extractedValue = wrappedInt.intValue();
        System.out.println(wrappedInt);
        System.out.println(extractedValue);
        System.out.println("---");
        
        // Auto-boxing and auto-unboxing (JDK 5+)
        Integer autoBoxed = 123;      // Auto-boxing
        int autoUnboxed = autoBoxed;  // Auto-unboxing
        
        // Converting String to int
        String numericString = "255";
        int convertedValue = Integer.parseInt(numericString);
        System.out.println("Converted: " + convertedValue);
        System.out.println("After addition: " + (convertedValue + 45));
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.