Close

Java Date Time - YearMonth.now() Examples

Java Date Time Java Java API 


Class:

java.time.YearMonth

java.lang.Objectjava.lang.Objectjava.time.YearMonthjava.time.YearMonthjava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Methods:

public static YearMonth now()

Obtains the current year-month from the system clock in the default time-zone.



public static YearMonth now(ZoneId zone)

Obtains the current year-month from the system clock in the specified time-zone.



public static YearMonth now(Clock clock)

Obtains the current year-month from the specified clock.


Examples


package com.logicbig.example.yearmonth;

import java.time.YearMonth;

public class NowExample {

public static void main(String... args) {
YearMonth y = YearMonth.now();
System.out.println(y);
}
}

Output

2017-05




package com.logicbig.example.yearmonth;

import java.time.YearMonth;
import java.time.ZoneId;

public class NowExample2 {

public static void main(String... args) {
YearMonth y = YearMonth.now(ZoneId.of("Africa/Lagos"));
System.out.println(y);
}
}

Output

2017-05




package com.logicbig.example.yearmonth;

import java.time.Clock;
import java.time.Duration;
import java.time.YearMonth;

public class NowExample3 {

public static void main(String... args) {
Clock c = Clock.tick(Clock.systemDefaultZone(), Duration.ofHours(20000));
System.out.println(c);

YearMonth y = YearMonth.now(c);
System.out.println(y);
}
}

Output

TickClock[SystemClock[America/Chicago],PT20000H]
2015-08




See Also