Terminate Java Thread

The best way to terminate a Java thread is with a switch such as a boolean property, .e.g abort. As soon as a condition is met, usually a timeout, you can change the value of abort to true and thus exit thread. Example: import java.util.concurrent.*; public class CheckResults { private static int counter = 0;… read more »

Referencing a Child Object to a Variable of Type Parent in Java

Referencing a child object to a variable of type parent in Java can be real confusing. Here is an example of two classes Person and Student and an overriden method whatAmI: class Person { String it = “It is a Person.”; public void whatAmI(Object x) { System.out.print(“Person’s object… ” + it); } } class Student… read more »

Working with Time and Date in Java

Working with time and date in Java is essential to any project. There are three important classes you can use: java.time.LocalTime – working only with time. java.time.LocalDate – working only with date. java.time.LocalDateTime – working with time and date both. All three classes provide two essential methods: of(arguments) – accepting arguments for the time/date/datetime values…. read more »

How to Parse Time in Java

To parse time in Java, the most trouble-free and programatic way is using the parse method of the LocalTime class like this: import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class TimeParse { public static void main(String[] args) { String inputStr = “Time is 11 minutes past 11PM.”; String pattern = “‘Time is ‘m’ minutes past ‘ha.”; DateTimeFormatter… read more »