I understand that in java you are able to write an external class that can be imported into a script and then run which allows that class to be used in multiple different places.
I was wondering if there is a way to do the same thing with a method. I find that sometimes I need to create an external class to do something very small and basic like the following.
simpleScript.java
public class simpleScript {
public static void main() {
// just a date variable which I will want to format
// but I will always one of two formats every time
// I use a date so I need a library/class function
Date dateNow = new Date();
// to use my library/class function I have to
// initialize it first
dateFormat localDateFormat = new dateFormat();
// now I can use it in the main only
String dmyNow = localDateFormat.dmyFormat(dateNow);
String timeNow = localDateFormat.timeFormat(dateNow);
}
}
dateFormat.java
public class dateFormat() {
public static String dmyFormat(Date dateArg) {
SimpleDateFormat dmyStyle = new SimpleDateFormat("dd-MM-yyyy");
String dateResult = dmyStyle.format(dateArg);
return dateResult;
}
public static String timeFormat(Date dateArg) {
SimpleDateFormat timeStyle = new SimpleDateFormat("hh:mm:ss");
String timeResult = timeStyle.format(dateArg);
return timeResult;
}
}
This doesn't bother me that much until it's something I need to use in multiple different methods, sometimes only for one date so i have to keep initializing the class to use the methods inside it (I don't want to make it a global class).
Is there another way to make my methods reusable without a class (across multiple java files)?