The conversion of the Western calendar → Japanese calendar is not successful in java

Asked 2 years ago, Updated 2 years ago, 33 views

I need to convert the Western calendar of the screen input value to the Japanese calendar, so I will ask you a question.
"I created it as shown below, but the input value ""month"" is +1."
Please tell me the solution.

Input value (hereinafter referred to as calender.set (2016,3,24); part) → 2016,3,24
Results → April 30, 2016

I wish it would be March next to me, but it will be April.

import java.text.*;
import java.util.*;

public class MyTest {
  public static void main(String[]args) {
    Locale defaultLocale=new Locale("ja", "JP", "JP");
    DateFormat JapaneseFormat = new SimpleDateFormat ("M-d-day-GGGy"),
            defaultLocale);

    Calendar calendar=Calendar.getInstance();
    calendar.set (2016, 3, 24);

    System.out.println(JapaneseFormat.format(calender.getTime()));
  }
}

I would like to convert the points to the Western calendar → Japanese calendar with arguments given from the outside, not the system date.

java

2022-09-30 18:24

2 Answers

Javadoc also has a quiet description, but the moon is 0 because of the beginning.

Calendar#set(int, int, int) (Java Platform SE8)

Month - The value used to set the MONTH calendar field. The Month value begins with 0 (January equals 0).

In order to do it in March,

calender.set (2016, 2, 24);

must be


2022-09-30 18:24

If it is JDK8 or later, you can also use java.time.chrono.JapaneseDate to obtain the Japanese calendar.This one seems to be able to set the same month.
java.time.chrono.JapaneseDate

DateTimeFormatter formatter=DateTimeFormatter.ofPattern("M-d-Gyy";
JapaneseDate JapaneseDate=JapaneseDate.of (2016, 3, 24);
System.out.println(JapaneseDate.format(formatter)));
// March 24, 2016


2022-09-30 18:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.