Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Title:        efa - elektronisches Fahrtenbuch fr Ruderer
 * Copyright:    Copyright (c) 2001-2011 by Nicolas Michael
 * Website:      http://efa.nmichael.de/
 * License:      GNU General Public License v2
 *
 * @author Nicolas Michael
 * @version 2
 */

import java.util.*;

public class Main {
    public static String date2String(Date date) {
        return date2String(date, true);
    }

    public static String date2String(Date date, boolean printTime) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) > 10000) {
            return "---";
        }
        return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
                + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + "." + makeTimeString(cal.get(Calendar.YEAR), 4)
                + (printTime
                        ? " " + makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
                                + makeTimeString(cal.get(Calendar.MINUTE), 2) + ":"
                                + makeTimeString(cal.get(Calendar.SECOND), 2)
                        : "");
    }

    private static String makeTimeString(int value, int chars) {
        String s = Integer.toString(value);
        while (s.length() < chars) {
            s = "0" + s;
        }
        return s;
    }
}