Get a long
number within the range 0 to the number of milliseconds from the epoch and apply the formatter to get the date string in the desired format. Then compare it with the date string of today.
Demo:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
String strToday = sdf.format(now);
int counter = 0;
while (true) {
// Instantiate Date with minutes of 8 digits
Date date = new Date(TimeUnit.MILLISECONDS
.convert(ThreadLocalRandom.current().nextLong(10000000, 100000000), TimeUnit.MINUTES));
String strDate = sdf.format(date);
if (Objects.equals(strToday, strDate)) {
for (int i = 1; i < 6; i++) {
System.out.println("The date is " + strDate + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
break;
} else {
counter++;
}
}
}
}
A sample run:
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
Note that the java.util
date-time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.*
Using the modern date-time API:
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
// Change the ZoneId as applicable e.g. ZoneId.of("Europe/London")
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime now = ZonedDateTime.now(zoneId);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ddMMuuuu", Locale.ENGLISH);
String strToday = dtf.format(now);
int counter = 0;
while (true) {
// Instantiate Date with minutes of 8 digits
LocalDate date = Instant
.ofEpochMilli(TimeUnit.MILLISECONDS
.convert(ThreadLocalRandom.current().nextLong(10000000, 100000000), TimeUnit.MINUTES))
.atZone(zoneId).toLocalDate();
String strDate = dtf.format(date);
if (Objects.equals(strToday, strDate)) {
for (int i = 1; i < 6; i++) {
System.out.println("The date is " + strDate + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
break;
} else {
counter++;
}
}
}
}
A sample run:
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.