Convert dates of any format to unix timestamp

I wrote a simple date utils class that will convert dates in any format that you specify in the supportedDateFormats set into a unix timestamp. A lot of people prefer to store dates in this way and have to work with more than one format, so I thought this might be useful.

package com.grimey;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

public final class DateUtils {
private static final Set supportedDateFormats = new HashSet(Arrays.asList(
"dd-MMM-yyyy HH:mm:ss",
"yyyy-MM-dd HH:mm:ss"

private DateUtils() {}

public static long dateTimeToUnixTimestamp(final String dateTime) {
return supportedDateFormats
.flatMap(format -> convert(dateTime, format)

private static Optional convert(final String dateTimeString, final String dateFormatString) {
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormatString);

try {
final LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, dateTimeFormatter);
final ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));
final long timestamp = zonedDateTime.toEpochSecond();

return Optional.of(timestamp);
} catch (Exception e) {
return Optional.empty();


Country name to ISO code mapper

I was looking for a library that allowed me to pass in a country name and get the ISO code for it.
Unfortunately I couldn’t find one, so I wrote a tiny bit of code to achieve this without having to create a huge hard coded map like most of the threads on Stack Overflow suggest.

Here’s the code:


import java.util.Arrays;
import java.util.Locale;
import java.util.Map;

final class CountryService {
private final Map countryToISOMap;

public CountryService() {
countryToISOMap = Arrays
.collect(Collectors.toMap(isoCode -> {
final Locale locale = new Locale("", isoCode);

return locale.getDisplayCountry();
}, isoCode -> isoCode));

public String countryToISO(final String country) {
return countryToISOMap.getOrDefault(country, "");

Using completable futures in Java

This week I needed to download a lot of files on a remote server using multi-threading.
I always try to avoid multi-threading and treat it as an absolute last option as it makes code significantly more complicated as well as introducing all the really hard to debug bugs that it brings (race conditions, thread starvation\locks etc).
Unfortunately, as the bottleneck was the network, I couldn’t optimise to speed up the solution. So after looking for an alternative, I gave in and looked up how to do multi-threading in Java.

After some research, I decided that out of all the various multi-threading API’s, the Callable Future API was the one to go with. This is because the code that needs to process the downloaded files should not execute until all files are present. The Callable Future API handles this case the best.

The first thing I needed to do was find out how many files were in the remote directory. This was achieved by using an SSH library call JSch (

Once I ran a ls command, I read the returned input stream into a hash set of strings and then chunked the hash set by the amount of threads I want to download the files. Each chunk of file names are fed to each thread to which they go off and retrieve those files using a SCP command.
When all the threads have completed their downloading, the Join function collects the reults (which is a SshDownloadResult class that encapsulates sucessfully and unsuccessfully downloaded files).

Here is the code:

final ExecutorService executorService = Executors.newFixedThreadPool(threadsCount);

final List downloadTasks = IntStream
.range(0, threadsCount)
.map((threadIndex) -> (Callable) () -> {
final HashSet filesNamesForCurrentThread =
return download(connectionSettings, absoluteDownloadFromDirectory,
downloadToDirectory, filesNamesForCurrentThread);

final List futures = downloadTasks
.map(task -> CompletableFuture.supplyAsync(() -> {
try { return; }
catch (Exception e) {
log.error("Download thread " + task.toString() + " threw an exception.
Message = " + e.getMessage(), "runUsingSsh");
return null;    // Todo: Handle this better.
}, executorService))

final List downloadResult = futures