Home>

Read the file that is java.
Date of establishment November 14, 1998
I'd like to read the file with this one line with a buffalo reader and convert the output to a Japanese calendar in Heisei years, but I am struggling.
Just a hint.

  • Answer # 1

      

    Buffalo reader

    This is not a Buffalo reader, but a "BufferedReader" error.

      

    I want to convert the Japanese calendar in Heisei year, but I am struggling.
      Just a hint.

    According to the current level of understanding.

    I don't know how to use BufferedReader
    Guguro. How to use Java BufferedReader

    I do n’t know how to extract just the date
    Guguro. Java start string removal/Java date string

    I don't know how to convert to Japanese history
    Guguro. Java history

    A lot of information that seems to be a hint should be rolling.


    You may not be able to write the code you want even if you investigate thoroughly and try it.
    Still, "I don't know" should gradually become clearer.

    Tips for asking questions

  • Answer # 2

    (The buffalo leader seems to be strong)

    Is the file reading unsuccessful or Japanese calendar conversion unsuccessful?

    By the way, even if you do not use a buffer reader, you can read a file in a simpler way with Java 7 ~, and there is an easy way to handle dates with Java 8 ~.

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.time.LocalDate;
    import java.time.chrono.JapaneseDate;
    import java.time.format.DateTimeFormatter;
    import java.util.List;
    import java.util.stream.Collectors;
    / **
     * @author a-pz
     *
     * /
    public class Wareki {
        / **
         * @param args
         * /
        public static void main (String [] args) throws Exception {
            Wareki wareki = new Wareki ();
            String loadString = wareki.readFromFile ("date.txt");
            LocalDate localDate = wareki.getLocalDate (loadString);
            String warekiString = wareki.getWareki (localDate);
            System.out.println (warekiString);
        }
        public String readFromFile (String filename) throws IOException {
            Path path = Paths.get (filename);
            List<String>lines = Files.readAllLines (path);
            return lines.stream (). collect (Collectors.joining (""));
        }
        public LocalDate getLocalDate (String dateString) {DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("uuuu year MM month dd day");
            LocalDate ldt = LocalDate.parse (dateString, formatter);
            return ldt;
        }
        public String getWareki (LocalDate ldt) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("GGGGyy year MM month dd day");
            JapaneseDate jpn = JapaneseDate.from (ldt);
            return jpn.format (formatter);
        }
    }

  • Answer # 3

    Date-related input/output and type conversion are often examples.
    When I tried to write it, there were many things that I couldn't write easily, so I tried to write my own review.
    I didn't know the file input and data specifications, so I tried to convert only the format that was in question.
    I hope it helps.


    Sample: Established November 14, 1998
    Processing shaping: 1998/11/14
    Processing disassembly: 1998 11 14
    Japanese calendar output: November 14, 1998

    import java.text.DateFormat;
    import java.text.ParsePosition;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    public class Sample {
        // Sample date conversion that seems to be used well for the time being
        public static void main (String [] args) {
            // Loop processing that reads from the file line by line (omitted)
            String strDate = "Established date 14 November 1998";
            // sample data
            System.out.println ("Sample:" + strDate);
            // shaping 1998/11/14
            strDate = strDate.substring (4);
            strDate = strDate.replace ("year", "/");
            strDate = strDate.replace ("Month", "/");
            strDate = strDate.replace ("day", "");
            System.out.println ("Processing shaping:" + strDate);
            // decomposition 1998 11 14
            int strY = Integer.parseInt (strDate.substring (0, 4));
            int strM = Integer.parseInt (strDate.substring (5, 7));
            int strD = Integer.parseInt (strDate.substring (8, 10));
            System.out.println ("Processing decomposition:" + strY + "" + strM + "" + strD);
            // String type>>>Date type conversion
            SimpleDateFormat fmt = new SimpleDateFormat ("yyyy/MM/dd");
            ParsePosition pos = new ParsePosition (0);
            Date dateDate = fmt.parse (strDate, pos);// [1]
            // Japanese calendar locale
            Locale locale = new Locale ("ja", "JP", "JP");
            // Date type>>>Japanese calendar conversion output
            DateFormat japaseseFormat = new SimpleDateFormat ("GGGGy year M month d", locale);
            String strDateJP = japaseseFormat.format (dateDate);
            System.out.println ("Japanese calendar output:" + strDateJP);
        }
    }