Skip to content

java.time.LocalDate로 날짜 표현하기 #12

Description

@shoon2430

현재 날짜 반환

LocalDate localDate=LocalDate.now();
System.out.println(localDate.toString());//(현재기준)2018-09-11 출력

현재 날짜의 다양한 요소 반환

System.out.println(localDate.getDayOfYear());//365일 중 며칠인지?
System.out.println(localDate.getDayOfMonth());//오늘이 며칠인지?, 254
System.out.println(localDate.getDayOfWeek());//무슨 요일인지?, 11
System.out.println(localDate.getDayOfWeek().getValue());//무슨 요일인지?(월-1 ~일-7), 2
System.out.println(localDate.getMonth());//September출력
System.out.println(localDate.getMonthValue());//9출력
System.out.println(localDate.getYear());//2018출력
System.out.println(localDate.lengthOfMonth());// 30, 31, 28, 29 출력

날짜 변형

System.out.println(localDate.plusDays(1));//하루 뒤 출력
System.out.println(localDate.plusWeeks(1));//한 주 뒤 출력
System.out.println(localDate.plusYears(1));//일년 뒤 출력
​
System.out.println(localDate.withDayOfMonth(8));//8일로 변경하여 출력 - 2018-09-08
System.out.println(localDate.withDayOfYear(12));///그 해의 12일로 변경하여 출력 - 2018-01-12
System.out.println(localDate.withMonth(10));//10월로 변경하여 출력 - 2018-10-11
System.out.println(localDate.withYear(2012));//2012년으로 변경하여 출력 - 2012-09-11

참고) java.joda.time.LocalDate

LocalDate currentDate = LocalDate.now();//현재 날짜 반환  
int curday=currentDate.getDayOfWeek();//요일
​
LocalDate afterTwoWeeks= currentDate.plusDays(15-curday+Integer.parseInt(day));//2주 후

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions