Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * Takes a date value as used in CPLC Date fields (represented by 2 bytes)
     * 
     * @param paramByte1
     * @param paramByte2
     * @throws IllegalArgumentException
     * @return
     */
    public static Date calculateCplcDate(byte[] dateBytes) throws IllegalArgumentException {
        if (dateBytes == null || dateBytes.length != 2) {
            throw new IllegalArgumentException("Error! CLCP Date values consist always of exactly 2 bytes");
        }
        // current time
        Calendar now = Calendar.getInstance();

        int year = now.get(Calendar.YEAR);
        int startYearOfCurrentDecade = year - (year % 10);

        int days = 100 * (dateBytes[0] & 0xF) + 10 * (0xF & dateBytes[1] >>> 4) + (dateBytes[1] & 0xF);

        if (days > 366) {
            throw new IllegalArgumentException("Invalid date (or are we parsing it wrong??)");
        }

        Calendar calculatedDate = Calendar.getInstance();
        calculatedDate.clear();
        calculatedDate.set(Calendar.YEAR, startYearOfCurrentDecade + (0xF & dateBytes[0] >>> 4));
        calculatedDate.set(Calendar.DAY_OF_YEAR, days);
        while (calculatedDate.after(now)) {
            calculatedDate.add(Calendar.YEAR, -10);
        }
        return calculatedDate.getTime();
    }
}