Algorithm Implementation/Date and time/Conversion of a Gregorian date to an International Fixed Calendar date
Appearance
This Source code converts a Gregorian Calendar date into a International Fixed Calendar date. The Gregorian date can be abbreviated, or
nothing entered for today's date. The program is in 2 classes: IFCConverter and TestIFCConverter.
Java
[edit | edit source]public class IFCConverter {
private String gregDate;//a gregorian date
private String IFCdate;//an International fixed calendar date
private static final String[] MONTHS = {"January", "February", "March", "April", "May", "June", "Midi", "July", "August", "September", "October", "November", "December"};//creates an constant array of months
//sets gregDate
public void setGregDate(String gD) {
if (gD.contains(" ") && !gD.startsWith(" ")) { //if the spaces won't //drive later code wacko
gregDate = gD;
}
}
//gets IFCdate
public String getIFCdate() {
return IFCdate;
}
public void GregIFC() {
String month = gregDate.substring(0, gregDate.indexOf(' '));//separates the Month part of the date
int monthNum = monthNum(month);//sets a month # equal to the Number of the //month (0 fo Jan., 1 for Feb., etc.)
int[] cfs = {0, 3, 3, 6, 8, 11, 13, 0, -12, -9, -7, -4, -2};//sets the conversion factors
String dayStr = gregDate.substring(gregDate.indexOf(' ') + 1);//separates the day of the month
int day = Integer.parseInt(dayStr);//parses the day string into a number
day += cfs[monthNum];//sets IFC day of month
if (day > 28 && monthNum != 12) {//if IFC date is greater than 28 and it //isn't the 29 day month
day -= 28;
month = MONTHS[monthNum + 1];//subtract 28 and go to the next month
}
if (day < 1) {//if IFC date is less than 1
day += 28;
month = MONTHS[monthNum - 1];//add 28 and go to previous month
}
IFCdate = month + " " + day;//sets IFCdate equal to final month and day
if (gregDate.equals("December 31")) {//because in the International Fixed Calendar, there's no Dec. //29
IFCdate = "Extra day";
}
}
public int monthNum(String month) {
String subMonth = month.substring(0, 3);//so abbreviations will work
for (int monthN = 0; monthN < MONTHS.length; monthN++) {//don't want to have the index be out of bounds
if (subMonth.equals(MONTHS[monthN].substring(0, 3))) {//if month equals current month
break;//leave monthN where it is
}
}
return monthN;
}
}
Test code
[edit | edit source]import java.util.*;
import java.text.*;
import java.io.*;
public class TestIFCConverter {
public static void main(String[] args) {
String input = TestIFCConverter.javaInput("enter a date:");//prompts the user to enter a date
IFCConverter ifcc = new IFCConverter();//makes a reference to IFCConverter
ifcc.setGregDate(input);//sets the Greg. date to the date string earlier made
if (input.equals("")) {
Date today = new Date();//Sets a variable equal to the current date
String date = DateFormat.getDateInstance(DateFormat.LONG).format(today);//formats today to MONTH Date, YEAR
ifcc.setGregDate(date.substring(0, date.length() - 6));//separates the year
}
ifcc.GregIFC();//converts the Greg. date to IFC
System.out.println(ifcc.getIFCdate());//prints the IFC Date
}
public static String javaInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
} catch (IOException e) {
System.err.println("IOException: " + e);
}
return inputLine;
}
}