Reputation: 31
LocalDateTime now = LocalDateTime.now();
How or where do I proceed from this line of code?
Upvotes: 1
Views: 687
Reputation: 9344
If you would like to code something more customizable or return especific info from a time you can use the Java Calendar Class, for example:
import java.util.Caldenar;
Calendar c = Calendar.getInstance();
int hour_now = c.get(Calendar.HOUR_OF_DAY); // returns the hour now as integer
Also, by using the LocalDate.of()
and the getDayOfWeek().name()
function is possible to get the name of the day. For example:
import java.time.LocalDate;
String name_day = LocalDate.of(2021, 01, 10).getDayOfWeek().name();
My code outputs the random time like this:
Check it out....
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Random;
public class randLast24Hours {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
Random rand = new Random();
int rand_hour;
int rand_minutes;
int today = c.get(Calendar.DAY_OF_MONTH); // get today's day of the month
int hour_now = c.get(Calendar.HOUR_OF_DAY); // hour now
int minutes_now = c.get(Calendar.MINUTE); // minutes now
int rand_day = rand.nextInt(2) + today - 1; // rand between yesterday & today
String name_day = LocalDate.of(2021, 01, rand_day).getDayOfWeek().name();
int rand_hour_yesterday = rand.nextInt(23-hour_now) + hour_now;
int rand_hour_today = rand.nextInt(hour_now+1);
// choose rand_hour and rand_minutes depending on what's the rand_day
if(rand_day == today){rand_hour = rand_hour_today;
rand_minutes = rand.nextInt(minutes_now);}
else {rand_hour = rand_hour_yesterday;
rand_minutes = rand.nextInt(60);}
int rand_seconds = rand.nextInt(60);
System.out.println("Random time for the last 24hrs:");
System.out.print(name_day+" ");if(rand_hour < 10) {System.out.print("0");}
System.out.print(rand_hour+":");if(rand_minutes < 10) {System.out.print("0");}
System.out.print(rand_minutes+":");if(rand_seconds < 10) {System.out.print("0");}
System.out.print(rand_seconds);}}
Upvotes: 0
Reputation: 1
Yeah I just saw that ChristianB posted a more random solution but im just gonna leave this here in case someone needs just random hours.
public static LocalDateTime minusRandomHours(int inputHours) {
int hours = new Random().nextInt(inputHours);
LocalDateTime randomTime = LocalDateTime.now().minusHours(hours);
return randomTime;
}
Upvotes: 0
Reputation: 2860
you can do it:
LocalDateTime.now().minusHours(new Random().nextInt(24));
if you need you similarly can add .minusMinutes(new Random().nextInt(60))
or .minusSeconds(new Random().nextInt(60))
Upvotes: 7
Reputation: 2680
This function gives you a LocalDateTime
within the last 24 hours. The random value is been taken based on the seconds of 24 hours:
import java.time.*;
import java.util.concurrent.ThreadLocalRandom;
public LocalDateTime randomLast24Hours() {
Long secondsOfDay = Duration.ofDays(1).getSeconds();
Long randomSeconds = ThreadLocalRandom.current().nextLong(secondsOfDay + 1);
return LocalDateTime.now().minusSeconds(randomSeconds);
}
Upvotes: 2