🚀 Java Date Formatting: Controlling Output with SimpleDateFormat ✍️ After covering the basic Date class, today I focused on formatting dates. The raw output of a Date object isn't always user-friendly, so the java.text.SimpleDateFormat class is essential for presenting dates in customized, readable ways. Key Formatting Patterns The SimpleDateFormat class uses specific pattern letters to define the output structure: Numeric Dates & Time: "dd/MM/yyyy HH:mm" (e.g., 14/11/2025 11:21): Uses numeric values for all components. Year-First Order: "yyyy/MM/dd" (e.g., 2025/11/14): Useful for file naming or strict international standards. Abbreviated Month/Day: "dd MMM yyyy" (e.g., 14 Nov 2025): Uses the short text representation for the month. Full Text Month: "dd MMMM yyyy" (e.g., 14 November 2025): Uses the full text name for the month. Day of the Week: "EEEE dd MMMM yyyy" (e.g., Friday 14 November 2025): Includes the full name of the day of the week. 🛠️ The Process The process is straightforward: Create a Date object (the data). Create a SimpleDateFormat object with the desired pattern (the presentation rule). Call the .format(Date obj) method to apply the rule and generate the formatted String. This control over presentation is vital for building clean user interfaces and standardizing data exchange! Thank you sir Anand Kumar Buddarapu,Saketh Kallepu,Uppugundla Sairam,Codegnan #Java #Programming #DateFormatting #SimpleDateFormat #JavaUtilities #TechEducation
How to Format Dates with SimpleDateFormat in Java
More Relevant Posts
-
💡 Custom Sorting in Java Using Comparator and TreeSet 🚀 In this Java project, I explored custom object sorting using multiple comparators — a powerful concept that enhances flexibility in data handling. 🧩 Key Concept: The program demonstrates how to sort user-defined objects (JobHolder) based on different attributes like id, name, department, dateOfBirth, and salary using the Comparator interface. 🌳 Core Idea: By passing different Comparator implementations to a TreeSet, we can automatically sort the elements according to custom rules. This approach is widely used in enterprise applications where data needs to be ordered dynamically — for example, sorting employee records, product catalogs, or transaction logs. ⚙️ What’s Inside: Implemented multiple comparator classes (SortById, SortByName, etc.) Created a TreeSet<JobHolder> with the desired sorting logic Observed how changing the comparator changes the sorting behavior 💬 Example Output: When sorting by Date of Birth, the employees are arranged in ascending order of their DOBs. 📚 Concepts Used: Comparator Interface 🏷️ TreeSet 🏷️Encapsulation 🏷️Object comparison 🏷️toString() overriding #Java #Comparator #TreeSet #CollectionsFramework #CodingJourney #LearningInPublic
To view or add a comment, sign in
-
💻 Today I Learned About Strings in Java In Java, Strings are a collection of characters enclosed within double quotes. They are objects and play a vital role in almost every Java program. There are two types of strings in Java: 🔹 Immutable Strings — Once created, they cannot be changed. Examples: name, date of birth, gender. 🔹 Mutable Strings — These can be modified. Examples: email ID, password, etc. For immutable strings, the class used is String. Strings created without using new keyword are stored in the String Constant Pool. Strings created using new keyword are stored in the Heap Memory. There are multiple ways to compare strings in Java: == → Compares references equals() → Compares values compareTo() → Compares character by character equalsIgnoreCase() → Compares values ignoring case differences Some commonly used String methods are: toLowerCase(), toUpperCase(), length(), charAt(), startsWith(), endsWith(), contains(), indexOf(), lastIndexOf(), and substring(). For mutable strings, we have two classes: StringBuffer → Synchronized, thread-safe, slower, suitable for multi-threaded environments. StringBuilder → Non-synchronized, not thread-safe, faster, suitable for single-threaded environments. ✨ Understanding strings is fundamental in Java, as they form the basis for text manipulation, data handling, and efficient memory management. #Java #String #Programming #Learning #JavaDeveloper #CodingJourney #TechLearning #SoftwareDevelopment #Immutable #Mutable #StringBuilder #StringBuffer
To view or add a comment, sign in
-
-
Loops in Java Loops in Java are used to execute a block of code repeatedly as long as a certain condition is true. They help reduce code repetition and make programs more efficient. Types of Loops in Java 1. for loop Used when the number of iterations is known. for (int i = 1; i <= 5; i++) { System.out.println(i); } Output: 1 2 3 4 5 2. while loop Used when the number of iterations is not fixed; condition is checked before execution. int i = 1; while (i <= 5) { System.out.println(i); i++; } 3. do-while loop Similar to while, but the loop body executes at least once even if condition is false. int i = 1; do { System.out.println(i); i++; } while (i <= 5);
To view or add a comment, sign in
-
-
💡 == Operator vs. .equals() Method: Why Context Matters in Java 🧐 When comparing two variables or objects in Java, the choice between the == operator and the .equals() method is critical. They perform two fundamentally different types of comparisons! 1. The == Operator (Identity Comparison) What it compares: The == operator always compares memory addresses (references). Primitives: When used with primitives (int, char, boolean, etc.), it checks if the values stored in those memory locations are identical. Example: (5 == 5) is true. Objects: When used with objects (including String), it checks if the two variables refer to the exact same object in the Heap memory. Example: (obj1 == obj2) is only true if they point to the same memory location (same object ID). 2. The .equals() Method (Content Comparison) What it compares: The .equals() method is used to check for content equality. It determines if two objects are meaningfully equal based on their data. Default Behavior: Since this method is inherited from the base Object class, its default behavior is the same as == (checking references). The Power of Overriding: For almost all custom classes and core classes (like String), this method is overridden. String overrides .equals() to check if the sequence of characters (the content) is identical. You must override it in your custom classes (like Employee) to define when two distinct objects are considered equal based on their field values (id, name, etc.). Always use .equals() when comparing the content of objects, and reserve == for comparing primitives or checking if two variables are references to the exact same physical object. Thank you sir Anand Kumar Buddarapu,Saketh Kallepu,Uppugundla Sairam,Codegnan #Java #ProgrammingTips #OOP #ObjectEquality #SoftwareDevelopment #TechEducation
To view or add a comment, sign in
-
-
💡 String vs. StringBuffer: Why Mutability Matters in Java 📝 When working with text in Java, understanding the core difference between String and StringBuffer—Mutability—is key to writing efficient code. 1. String (Immutable) Immutability: Once a String object is created, its value cannot be changed. Behavior: Any operation that appears to modify a String (like concatenation using the + operator) actually creates a brand new String object in the Heap memory. Performance: This continuous creation of new objects is slow and consumes extra memory, especially when concatenating strings repeatedly within a loop. Use Case: Ideal for storing text that is constant and will not change (e.g., names, final identifiers, or configuration values). 2. StringBuffer (Mutable & Synchronized) Mutability: The value of a StringBuffer object can be changed in the same memory location. Behavior: Methods like append(), insert(), or delete() modify the sequence of characters directly within the existing object's allocated memory buffer. No new object is created for intermediate changes. Synchronization: StringBuffer is thread-safe (synchronized), meaning its methods can be safely used by multiple threads simultaneously without causing data corruption. Performance: Much faster than String for repeated text manipulation because it avoids the overhead of creating numerous temporary objects. Use Case: Ideal for text manipulation in a multi-threaded environment where concurrent access to the string data is a concern. The Golden Rule: If the text is constant, use String. If the text needs to be repeatedly changed (modified, appended, or inserted into), use StringBuffer. Thank you sir Anand Kumar Buddarapu,Saketh Kallepu,Uppugundla Sairam,Codegnan #Java #ProgrammingTips #String #StringBuffer #Codegnan
To view or add a comment, sign in
-
-
Input in Java using scanner class. 1. Input in Java is used to take data from the user during program execution. 2. The most common way to take input is by using the Scanner class from the java.util package. 3. The Scanner class provides methods to read different data types like nextInt(), nextDouble(), nextLine(), next(), etc. 4. To use Scanner, it must be created as an object — Scanner sc = new Scanner(System.in); 5. System.in represents the standard input stream (keyboard). 6. After taking inputs, it is a good practice to close the Scanner using sc.close(). 7. Other ways to take input include BufferedReader and Console classes. 8. BufferedReader is faster and suitable for reading text data efficiently. 9. Console class is mainly used for reading password input securely (without echoing characters).
To view or add a comment, sign in
-
-
☕ Day 10 of my “Java from Scratch” Series – “Operators in Java” In Java, operators are used to perform operations between variables. We perform operations on variables (operands) instead of directly on values. 📘 Example: a + b; Here, ‘a’ and ‘b’ are “Operands”, and ‘+’ is the “Operator”. 🔹 Types of Operators in Java 1️⃣ Arithmetic Operators 2️⃣ Relational Operators 3️⃣ Assignment Operators 4️⃣ Unary Operators 5️⃣ Logical Operators 1. Arithmetic Operators: Operator Meaning + Addition - Subtraction * Multiplication / Division % Modulo (Remainder) 📘 Examples: 5 + 10 => 15 (addition) 10 - 5 => 5 (subtraction) 11 / 2 => 5 (quotient) 11 % 2 => 1 (remainder) 9 * 2 => 18 (multiplication) 🧩 String Concatenation: When we add two strings, concatenation happens. Eg: String add = "a" + "b"; ✅ Result: "ab" When we add an int value to a String, the int is converted to String automatically. int a = 5; String result = "ab" + a; ✅ Result: "ab5" If two int values are concatenated with a String, the numeric operation happens first, then the concatenation. int a = 5; int b = 20; System.out.println(a + b + "ab"); ✅ Result: "25ab" 💡 Java performs operations from left to right. ⚠️ A Few Important Points: ❌ You cannot subtract a number from a String. ✅ You can subtract a number from a char — because chars have ASCII values. Example: int b = 20; System.out.println('a' - b); // 97 - 20 = 77 💡 In short: Operators help us perform arithmetic, relational, logical, and assignment operations efficiently — and Java handles them from left to right. #Java #Programming #Coding #Learning #SoftwareEngineering #JavaDeveloper #Operators #JavaFromScratch #InterviewQuestions #Tech #ArithmeticOperatorsInJava #NeverGiveUp
To view or add a comment, sign in
-
💡 Understanding Interfaces in Java: 1)In Java, an Interface is a blueprint of a class. 2)It defines abstract methods that must be implemented by the class that uses it. 3)Interfaces help achieve abstraction, polymorphism, and multiple inheritance. 🧩 Example: interface Vehicle { void start(); void stop(); } class Car implements Vehicle { public void start() { System.out.println("Car started"); } public void stop() { System.out.println("Car stopped"); } } ⚙️ Types of Interfaces in Java: 1️⃣ Normal Interface 👉 Contains two or more abstract methods. Used commonly in real-world applications. 2️⃣ Functional Interface 👉 Contains only one abstract method. (Example: Runnable, Comparable) ✅ Used in Lambda Expressions. 3️⃣ Marker Interface 👉 Has no methods or fields. Used to mark or tag a class. (Example: Serializable, Cloneable). 4️⃣ SAM Interface (Single Abstract Method) 👉 Another name for a Functional Interface — ensures only one abstract method exists. 💬 Why Use Interfaces? ✔ Promotes loose coupling ✔ Makes code scalable and flexible ✔ Enables multiple inheritance #Java #OOP #Interface #Programming #Coding #TechLearning #JavaDeveloper #SoftwareEngineering #
To view or add a comment, sign in
-
🔗 Day 17: LinkedList in Java Today, I explored LinkedList in Java, an important data structure that allows dynamic insertion and deletion of elements efficiently. 💡 What I Learned Today LinkedList is part of the java.util package. It implements both List and Deque interfaces. Elements are stored as nodes, each linked to the next and previous ones. Fast insertion and deletion, slower random access compared to ArrayList. Can be used as List, Queue, or Deque. 🧩 Example Code import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { LinkedList<String> names = new LinkedList<>(); names.add("Raj"); names.add("Arun"); names.addFirst("Kumar"); names.addLast("Devi"); System.out.println("LinkedList: " + names); names.removeFirst(); names.removeLast(); System.out.println("After removal: " + names); } } ⚔️ Difference Between ArrayList and LinkedList ArrayList → Best for random access. LinkedList → Best for frequent insertions or deletions. 🗣️ LinkedIn Caption 🔗 Day 17 – Understanding LinkedList in Java Learned how LinkedList stores data in connected nodes and shines in insertion/deletion tasks. Also explored how it differs from ArrayList in speed and structure. Another key milestone in my #30DaysOfJava journey 🚀 #Java #CoreJava #LinkedList #LearnJava #Programming
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development