initial commit

This commit is contained in:
2022-04-14 13:57:08 +02:00
parent a6ae1323d2
commit fb69c498bf
21 changed files with 1209 additions and 5 deletions

View File

@ -0,0 +1,83 @@
package hevs.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Pierre-Andr<64> Mudry, HES-SO Valais 2010
* @version 1.0
*/
public class DateUtils {
private static final long msPerHour = 60 * 60 * 1000;
private static final long msPerDay = 24 * msPerHour;
/**
* Creates a date from a text representation of this date
* @param s The text representation, formatted as "dd/MM/yyyy"
* @return The corresponding date
*/
static public Date createDate(String s){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date theDate = null;
try {
theDate = sdf.parse(s);
}
catch(ParseException ex) {
System.err.println("Invalid date format specified !");
ex.printStackTrace();
}
return theDate;
}
static private long nMsec(Date a, Date b){
if(b.after(a)){
return b.getTime() - a.getTime();
}
else
{
return a.getTime() - b.getTime();
}
}
/**
* Computes the number of hours between two dates
* @param a The first date
* @param b The second date
* @return The number of hours between the two dates
*/
static public int nHours(Date a, Date b){
int n= Math.round(nMsec(a, b)/ msPerHour);
return n;
}
/**
* Computes the number of day between two dates
* @param a The first date
* @param b The second date
* @return The number of days between the two dates
*/
static public int nDays(Date a, Date b){
int nDays= Math.round(nMsec(a, b)/ msPerDay);
return nDays;
}
// Some samples
public static void main(String args[]){
Date first = DateUtils.createDate("1/1/2000");
Date second = DateUtils.createDate("1/1/2001");
// It also takes into account leap years (2000)
System.out.println("There were " + DateUtils.nDays(first, second) + " days in 2000");
// How old are you in days ?
Date birthdate = DateUtils.createDate("12/10/1977");
Date now = new Date();
System.out.println("You are " + DateUtils.nDays(now, birthdate) + " days old");
}
}

135
src/hevs/utils/Input.java Normal file
View File

@ -0,0 +1,135 @@
package hevs.utils;
import java.io.*;
/**
* * The Class Input is here to enter data with the keyboard.<br>
* The types below are supported by the Input class. <br>
* <br>
* - String <br>
* - Integer (int) <br>
* - Double (double) - Boolean (boolean) <br>
* - Character (char) <br>
* <br>
* <br>
*
* @author Patrice Rudaz (patrice.rudaz@hevs.ch)
* @author Cathy Berthouzoz (cathy.berthouzoz@hevs.ch)
* @modified Pierre-Andr<64> Mudry
* @version 3.0 - 6-10-2008
* @see #readString()
* @see #readLong()
* @see #readLong()
* @see #readDouble()
* @see #readBoolean()
* @see #readChar()
*/
public class Input {
/**
* * Reads a valid char value from the console.
*
* @return The typed char
* @see java.lang.Character
*/
public static char readChar() {
boolean ok = false;
int res = -1;
while (!ok) {
try {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
res = stdin.read();
ok = Character.isDefined(res);
} catch (Exception ex) {
System.out.println("This is not a valid character. Try again");
}
}
return (char) res;
}
/**
* * Reads a String from the console.
*
* @return The typed string
* @see java.lang.String
*/
public static String readString() {
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
try {
return stdin.readLine();
} catch (Exception ex) {
return "There is a problem. Try again.";
}
}
/**
* * Reads a valid integer value from the console.
*
* @return The typed value
* @see java.lang.Integer
*/
public static int readInt() {
boolean ok = false;
int res = -1;
while (!ok) {
try {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
String s = stdin.readLine();
if (s.startsWith("0x") || s.startsWith("0X")) {
res = Integer.parseInt(s.substring(2), 16);
} else {
res = Integer.parseInt(s, 10);
}
ok = true;
} catch (Exception ex) {
System.out.println("This is not a valid number. Try again");
}
}
return res;
}
/**
* * Reads a valid double value from the console.
*
* @return The typed double value
* @see java.lang.Double
*/
public static double readDouble() {
boolean ok = false;
double res = -1;
while (!ok) {
try {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
res = Double.parseDouble(stdin.readLine());
ok = true;
} catch (Exception ex) {
System.out.println("This is not a valid number. Try again");
}
}
return res;
}
/**
* * Reads a valid boolean value from the console.
*
* @return the value true if the typed value is true, false otherwise.
* @see java.lang.Boolean
*/
public static boolean readBoolean() {
boolean ok = false;
boolean res = false;
while (!ok) {
try {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
res = Boolean.parseBoolean(stdin.readLine());
ok = true;
} catch (Exception ex) {
System.out.println("This is not a valid boolean. Try again");
}
}
return res;
}
}