Skip to content

Small tasks

Parsing a date string to ISO8601 with Joda Time

Recently, I faced a date conversion task: convert a string date “yyyy-MM-ddZ” (i.e. “1983-09-15+03:00”) to the ISO8601 standard “yyyy-MM-dd’T’HH:mm:ss.SSSZ” (i.e. “1983-09-15T03:00:00.000+03:00”).

I used Apache Joda time to convert date. First, add a Maven dependency in pom.xml

<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.4</version>
</dependency>

Read More →

Java task. Fibonacci sequence.

Fibonacci numbers are a number sequence, where first two numbers are 1 and 1 or 0 and 1. Each subsequent number is the sum of previous two numbers. This task is often asked by interviewers (not only in Java). Let’s solve it in two ways: using a loop and recursion.

public int getFibonacci(int number) {
    if (number == 1 || number == 2) {
        return 1;
    }
		
    int fibo1 = 1;
    int fibo2 = 1;
    int fibonacci = 1;
		
    for (int i = 3; i <= number; i++) {
        fibonacci = fibo1 + fibo2;
        fibo1 = fibo2;
        fibo2 = fibonacci;
    }

    return fibonacci;
}

To see a recursive solution of this task, click “read more”.

Read More →