Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date and week randomizer

Status
Not open for further replies.

Xsi

Programmer
May 29, 2015
121
SE
Hello everyone,

I need to create a a simple java function where I can put start and end datum in the format 201901 up to 202052
that means every year have got 52 week. so the randomizer put a value between year 2019 and 2020. also a week between 01 to 52.
and the format need to look like

201901

my atempt is below but how do I keep the leading zero ?

Java:
public static void main(String[]args)
	{
		
			public static void main(String[]args)
	{
		
		int weekMin=01;
		int weekMax=52;
		int YearMin=2019;
		int YearMax=2020;
		int randomWeek = ThreadLocalRandom.current().nextInt(weekMin, weekMax + 1);
		int randomYear = ThreadLocalRandom.current().nextInt(YearMin, YearMax + 1);
		String Week = String.format("%02d", randomWeek);
		
				System.out.println(randomYear+""+Week);
		
	}


	}

Thank you in advance
 
Hi

Before you get deeper into this approach, I would like to bring in your attention that not all years have 52 weeks. For example 2015 and 2020 have 53.


Feherke.
feherke.github.io
 
as I mentioned its between 2019 and 2020 @feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top