Programming Java

Import CSV

Download csv

Truy cập vào link sau để tải file csv download csv

Read data from InputStream

BufferedReader br = new BufferedReader(new InputStreamReader(f.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = br.readLine()) != null) {
    line = br.readLine();
}

Parse Date

SimpleDateFormat  formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter.parse("2024-11-25");

Parse CSV

Regular Expression
final Pattern csvPattern = Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
String str = "name,\"John, Doe\",age,\"30\",city,\"New York\"";
String[] arr = csvPattern.split(str);

Format date in View Thymeleaf

<td th:text="${#dates.format(o.date, 'dd/MM/yyyy')}"></td>

For i in View Thymeleaf

<ul>
    <li th:each="i: ${#numbers.sequence(1, 10)}">
        <a th:href="@{/customer?page={page}(page=${i})}" th:text="${i}"></a>
    </li>
</ul>

Customer Repository Pagination

public interface CustomerRepository extends PagingAndSortingRepository<Customer, String>, CrudRepository<Customer, String>{
}

metho Pagination

@GetMapping("/customer/{page}")
public String index(Model model, @PathVariable int page){
    Pageable pageable = PageRequest.of(page, PageSize);
    Iterable list = customerRepository.findAll(pageable);
    model.addAttribute("list", list);
    return "customer/index";
}