Java.Java8.How to get current time in local time with millisecond precision using Date Time API?

Let’s now get the current local time (i.e., your system time zone) with millisecond precision using the Java 8 Date Time API.


✅ Best Way: Use LocalTime.now() + Format with .SSS

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class CurrentTimeMillisPrecision {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();

        String formatted = time.format(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"));

        System.out.println("Local time with milliseconds: " + formatted);
    }
}

🔹 Output:

Local time with milliseconds: 14:52:31.128

🔧 Why It Works:

PartDescription
LocalTime.now()Gets current local system time
DateTimeFormatter"HH:mm:ss.SSS" includes milliseconds
SSSMilliseconds (3 digits, padded)

✅ If You Also Want the Date:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

LocalDateTime now = LocalDateTime.now();
String formatted = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));

System.out.println("Local date & time with milliseconds: " + formatted);

Posted in Без рубрики | Comments Off on Java.Java8.How to get current time in local time with millisecond precision using Date Time API?

Java.Java8.How to get current time with millisecond precision using Date Time API?

✅ Use LocalTime.now() or LocalDateTime.now()

🔹 Example with LocalTime:

import java.time.LocalTime;

public class TimeWithMillis {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        System.out.println("Current time: " + time);
    }
}

🔹 Output:

Current time: 14:45:12.123

✅ More Common: Use LocalDateTime (includes date too)

import java.time.LocalDateTime;

public class TimeWithMillis {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("Current datetime: " + now);
    }
}

🧠 Want to Extract Just the Milliseconds?

int millis = LocalTime.now().getNano() / 1_000_000;
System.out.println("Milliseconds: " + millis);

⚠️ getNano() returns nanoseconds — we divide by 1,000,000 to get milliseconds.

🔸 Optional: Format It Nicely

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

LocalTime time = LocalTime.now();
String formatted = time.format(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"));
System.out.println("Formatted: " + formatted);

✅ Output:

Formatted: 14:45:12.123
Posted in Без рубрики | Comments Off on Java.Java8.How to get current time with millisecond precision using Date Time API?

Java.Java8.How to get second Saturday of current month using Date Time API?

✅ Step-by-step Approach

  1. Get the first day of the current month
  2. Use TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SATURDAY) to find the 2nd Saturday

✅ Full Example:

import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.temporal.TemporalAdjusters;

public class SecondSaturday {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        LocalDate secondSaturday = today
            .withDayOfMonth(1) // first day of current month
            .with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SATURDAY)); // 2nd Saturday

        System.out.println("Today:            " + today);
        System.out.println("Second Saturday:  " + secondSaturday);
    }
}

🔹 Output Example (assuming today is April 15, 2025):

Today:            2025-04-15
Second Saturday:  2025-04-12

🧠 Tip:

If you want the first Monday, third Sunday, or last Friday, just change:

TemporalAdjusters.dayOfWeekInMonth(N, DayOfWeek.X)

Or use:

TemporalAdjusters.lastInMonth(DayOfWeek.X)
Posted in Без рубрики | Comments Off on Java.Java8.How to get second Saturday of current month using Date Time API?

Java.Java8.How to get next Tuesday using Date Time API?

✅ Use TemporalAdjusters.next(DayOfWeek.TUESDAY)

import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.temporal.TemporalAdjusters;

public class NextTuesdayExample {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        LocalDate nextTuesday = today.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));

        System.out.println("Today:        " + today);
        System.out.println("Next Tuesday: " + nextTuesday);
    }
}

🔹 Output Example:

Today:        2025-04-15
Next Tuesday: 2025-04-22

🔸 Bonus: If Today Is Tuesday and You Want the Next One

  • TemporalAdjusters.next() → always moves to the next one, even if today is Tuesday
  • TemporalAdjusters.nextOrSame() → includes today if it’s already Tuesday
LocalDate nextOrSameTuesday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
Posted in Без рубрики | Comments Off on Java.Java8.How to get next Tuesday using Date Time API?

Java.Java8.How to add 1 week, 1 month, 1 year, 10 years to current date using Date Time API?

🗓️ Java 8’s java.time API makes adding time units to dates super clean and readable using methods like .plusWeeks(), .plusMonths(), .plusYears().

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.How to add 1 week, 1 month, 1 year, 10 years to current date using Date Time API?

Java.Java8.How to get current date using Date Time API from Java 8?

✅ Ways to Get the Current Date

🔹 1. Using LocalDate

If you want just the date (without time):

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current date: " + currentDate);
    }
}
Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.How to get current date using Date Time API from Java 8?

Java.Java8.What is ZonedDateTime?

⏰ Let’s talk about ZonedDateTime, the most precise and complete date-time class in Java 8’s java.time package.

✅ What is ZonedDateTime?

ZonedDateTime is a class that represents:

📅 Date + 🕒 Time + 🌍 Time Zone

🔹 Example:

2025-04-15T10:30:00+05:00[Asia/Yekaterinburg]

It contains:

  • LocalDateTime → date + time
  • Zone offset → e.g., +05:00
  • Zone ID → e.g., Asia/Yekaterinburg
Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What is ZonedDateTime?

Java.Java8.What are terminal methods and what are not ?

✅ 1. Intermediate Methods (NOT terminal)

  • These build up the stream pipeline
  • Lazy – they don’t do anything until a terminal method is called
  • Always return a Stream (or IntStream, etc.)

🔹 Examples:

filter(), map(), flatMap(), sorted(), distinct(), limit(), skip(), peek()

These methods transform the stream and are usually chained.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What are terminal methods and what are not ?

Java.Java8.✅ What Is LocalDateTime?

LocalDateTime represents a date and time without a time zone — basically a timestamp like:

2025-04-15T08:30:00

It combines:

  • LocalDate → date (year, month, day)
  • LocalTime → time (hour, minute, second)

🔹 When to Use It

Use LocalDateTime when:

  • You need a full date & time, but
  • You don’t care about time zones or UTC offsets

Perfect for:

  • Logging timestamps
  • Scheduling (e.g., “April 15 at 8:30”)
  • Local clock time (like showing system time)

✅ Example

import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("Current date & time: " + now);
    }
}

🔹 Output:

Current date & time: 2025-04-15T08:30:12.345

🔧 Common Methods

MethodPurpose
now()Current date & time
of(y, m, d, h, m)Create a specific date-time
plusDays(), minusHours()Add/subtract time
getDayOfWeek()Day of the week (e.g., MONDAY)
toLocalDate()Extract date only
toLocalTime()Extract time only
format(...)Format using DateTimeFormatter

🧠 Bonus: Formatting Example

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

LocalDateTime now = LocalDateTime.now();
String formatted = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
System.out.println(formatted); // e.g. 2025-04-15 08:30
Posted in Без рубрики | Comments Off on Java.Java8.✅ What Is LocalDateTime?

Java.Java8.What additional methods for working with associative arrays (maps) appeared in Java 8?

Java 8 brought some powerful new methods to Map that made working with associative arrays (maps) much cleaner, safer, and more expressive. 💡 Let’s go over the most useful ones:


✅ New Map Methods in Java 8

MethodPurpose
getOrDefault()Returns value if present, or default value if key is missing
putIfAbsent()Puts a value only if the key is not already associated with one
compute(), computeIfAbsent(), computeIfPresent()Dynamically compute values
merge()Merges values in a map using a remapping function
forEach()Iterates over entries using a BiConsumer
replace()Replaces value for a key (optional conditional version)
replaceAll()Replaces all values using a BiFunction
remove(key, value)Removes entry only if key maps to that exact value

🔍 Examples of Each

Continue reading →
Posted in Без рубрики | Comments Off on Java.Java8.What additional methods for working with associative arrays (maps) appeared in Java 8?