J
J
Jsman2016-03-03 00:24:01
Java
Jsman, 2016-03-03 00:24:01

How to replace all values?

Goodnight ! There is a string in the cat th contains a date of this format
12-Dec-2015
13-Jan-2015, etc.
I don’t understand how to change the entire value to numeric?
I tried replase, but it turns out that the lines are duplicated =(

for(int i=0; i <= listOne.getLastRowNum(); i++){
            String xlsxList = listOne.getRow(i).getCell(1).getStringCellValue();

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-03-03
@SeniorDmitry

You should learn to ask questions. The staging is so bad that one can only speculate and speculate. Judging by the attached code, the data is taken from Excel. Apparently, the 1st cell of each row contains a string value like "12-Dec-2015". It is required to replace the three-character month designations with numeric ones.

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Iterator;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class DateFormatDemo {
    private static final Map<String, String> months;

    static {
        months = new HashMap<String, String>();
        months.put("янв", "01");
        months.put("фев", "02");
        months.put("мар", "03");
        months.put("апр", "04");
        months.put("май", "05");
        months.put("июн", "06");
        months.put("июл", "07");
        months.put("авг", "08");
        months.put("сен", "09");
        months.put("окт", "10");
        months.put("ноя", "11");
        months.put("дек", "12");
    }

    private static String changeFormat(String in) {
        int startPosition = in.indexOf('-');
        if(startPosition != -1) {
            int endPosition = in.indexOf('-', startPosition + 1);
            if(endPosition != -1) {
                String strMonth = in.substring(startPosition + 1, endPosition);
                if(months.containsKey(strMonth)) {
                    String intMonth = months.get(strMonth);
                    return in.replace(strMonth, intMonth);
                }
            }
        }
        throw new IllegalArgumentException("Invalid date format");
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        if(args.length < 1) {
            System.err.println("Missing filename!");
            return;
        }

        InputStream in = new FileInputStream(args[0]);
        HSSFWorkbook wb = new HSSFWorkbook(in);
        Sheet sheet = wb.getSheetAt(0);
        Iterator<Row> it = sheet.iterator();

        Spliterator<Row> spliterator = Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED);
        Stream<Row> stream = StreamSupport.stream(spliterator, false);

        stream
            .map(r -> r.getCell(1).getStringCellValue())
            .filter(s -> s.length() > 0)
            .map(DateFormatDemo::changeFormat)
            .forEach(System.out::println);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question