finnish snippets

Parse a date in java

Tagged finnish, date parser, java, pã¤ivã¤mã¤ã¤rã¤n parsiminen, parsering av datum  Languages java

A simple class that parses a date given in finnish formats d.m.yyyy, dd.mm.yyyy and ddmmyyyy.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateParser {

  Calendar calendar = null;

  public DateParser(String dateAsString) {
    if ( dateAsString == null || dateAsString.length() < 8 ) {
      throw new RuntimeException("String must be at least 8 characters long and in one of these formats: ddmmyyyy | d.m.yyyy | dd.mm.yyyy");
    }
    dateAsString = dateAsString.replaceAll("\\.","");
    String parseString = "ddMMyyyy";
    if ( dateAsString.length() < 8 ) {
      parseString="dMyyyy";
    }
    try {
      Date date = new SimpleDateFormat(parseString).parse(dateAsString);
      calendar = Calendar.getInstance();
      calendar.setTime(date);
    } catch (Exception e) {
      throw new RuntimeException("String must be at least 8 characters long and in one of these formats: ddmmyyyy | d.m.yyyy | dd.mm.yyyy",e);
    }
  }

  public int getDayOfMonth() {
    return calendar.get(Calendar.DATE);
  }

  public int getMonth() {
    return calendar.get(Calendar.MONTH) + 1;
  }

  public int getYear() {
    return calendar.get(Calendar.YEAR);
  }

  public Calendar getTime() {
    return calendar;
  }

}

Finnish SSID/HETU generator in Ruby

Tagged ruby, hetu, ssid, finnish, finland  Languages ruby
module Ssid
  module Fake
    #
    # Generates fake Finnish SSIDs (HETU).
    #
    # Original Python version:
    # https://gist.github.com/puumuki/11172310
    #
    class Finnish
      CHECK_KEYS = "0123456789ABCDEFHJKLMNPRSTUVWXY"
      CENTURIES = { '18' => '+',
                    '19' => '-',
                    '20' => 'A' }
      def self.generate(start_year=1800, end_year=2014)
        year = start_year + rand(start_year - end_year)
        month = 1 + rand(12)
        day = 1 + rand(days_in_month(year, month))
        century_sep = CENTURIES[year.to_s[0..1]]
        order_num = 2 + rand(889)
        check_number = "%02d%02d%s%03d" % [ day, month, year.to_s[0..1], order_num ]
        check_number_index = check_number.to_i % 31
        key = CHECK_KEYS[check_number_index]
        "%02d%02d%s%s%03d%s" % [ day, month, year.to_s[0..1], century_sep, order_num, key ]
      end

      private

      def self.days_in_month(year, month)
        (Date.new(year, 12, 31) << (12-month)).day
      end
    end
  end
end