Run a java function after a specific number of seconds

new java.util.Timer().schedule( new java.util.TimerTask() { @Override public void run() { // your code here } }, 5000 ); EDIT: javadoc says: After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer’s task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can … Read more

How can I parse/format dates with LocalDateTime? (Java 8)

Parsing date and time To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern. String str = “1986-04-08 12:30”; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm”); LocalDateTime dateTime = LocalDateTime.parse(str, formatter); Formatting date and … Read more

How to validate a JTextField?

Any validation in Swing can be performed using an InputVerifier. 1. First create your own input verifier: public class MyInputVerifier extends InputVerifier { @Override public boolean verify(JComponent input) { String text = ((JTextField) input).getText(); try { BigDecimal value = new BigDecimal(text); return (value.scale() <= Math.abs(4)); } catch (NumberFormatException e) { return false; } } } … Read more

Sending and receiving UDP packets?

The receiver must set port of receiver to match port set in sender DatagramPacket. For debugging, try listening on port > 1024 (e.g. 5000 or 9000). Ports < 1024 are normally used by system services and need admin access to bind on such a port. If the receiver sends packet to the hard-coded port it’s … Read more

java.lang.ClassNotFoundException: org.postgresql.Driver, Android

You need to add the PostgreSQL JDBC Driver in your project as mentioned in search.maven.org. Gradle: implementation ‘org.postgresql:postgresql:42.2.10’ Maven: <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.10</version> </dependency> You can also download the JAR and import to your project manually. NOTE: Compile as used in this answer is deprecated. Replace with implementation as per 3.