Creating a unique date each time between calls - java

I am writing a Java program using selenium. I need a function that returns a unique date (mm/dd/yyyy) each time it is called. The conditions though are that
It can never return a date it returned before
Is must return a date between 01/01/2071 and 12/31/9999
The program will run many times so all program memory will be lost upon termination. It
must remember the dates it has returned before. See next item
The easiest way to do this is just keep incrementing the date by 1 day each time, so it
needs to remember only 1 date.
Unfortunately I cannot write the last date returned to a file in the system to read it
next time the program runs because I do not have that ability.
The program will be reading data from an Excel spreadsheet so could theoretically store
the latest date in a cell, but the spreadsheet will be open and it does not seem to
have the ability to write to an open file.
Any thoughts? One thing I thought about doing was using a base date like 1/1/2014 at 00:00:00 and then taking the current date, calculating the number of minutes between the two, and adding this as a number of days to 11/31/2070. Unfortunately this would work only a couple of years because then there would be more minutes between the two dates than there are days from 1/1/2017 to 12/31/9999
Any help is appreciated.
Thanks

Related

Is there any way to code the time limit?

I'm working on quiz type app in which I want to give user only few turns to give an answer and after exceeding that count he have to wait 10 hours of time limit to try again.
So is there any class/method that may help me to set that time limit in Android?
You can simply store the date where the user starts to take the quizz on the application, and compare the current date with it when needed, the problem is that the user may change the datetime of its device to bypass the limit, so is better to check server side for current dates.
Save on database or SharedPreferences and compare latter, the server side is for getting the current real date toa void user changing it device date to bypass your validation.

Generate a code for records in data base

I'm facing problem which I can't name so its hard to google it! :)
Technological stack of my project is Java 8, Spring Boot 2.0.0.M4, MongoDB 3.4.7
So what I would like to ask You:
In my web application I need generate a code for records in data base.
Code should look like this YYMMDD0001 (1709290001) first part YYMMDD is easy.
But what about second part 0001 and then 0002 - 9999. The problem is, I have many users, so this number have to be shared. Number also must be reset every next day to 0001. If I'll restart server I have to get last value back. It's obvious that for this I'll have to create kind of a document in MongoDB.
Maybe You know some good approach how to do this, to make it possible to work with many users.
What is a popper name for such approach?
Start here:
https://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/
Then expand the code to track a stored date along with the seq num. The vendNextId() function will call date() and if the date crosses into the next day, reset the seq to 0 and set the stored date to the new date.

Where does JVM gets the date and Prints it? [duplicate]

This question already has answers here:
How can I ensure in Java that the current local time is correct? [duplicate]
(3 answers)
Closed 6 years ago.
I stumbled upon a idea below and wanted to know your opinion:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
If the user's computer is set to a wrong date, does this is print out wrong date.
From where does the JVM captures the date ? How to make sure that Date is always printed correctly even when the end user has adjusted his date?
java relies on the system clock, so if the user has misconfigured their time, timezone, date etc that is what you'll see.
having said that, if your code can assume a working internet connection you could maybe use some java NTP implementation to connect to internet time servers and get the correct time.
see more info here - How to use an Internet time server to get the time?
if you dont require the degree of precision that NTP offers and want something simpler you could hit up any number of rest APIs that serve time - here is one
JVM will capture the date from the System. If you set it incorrect it will give you incorrect timings. Still if you would like to have correct timings, you can use any REST API's which will pick up correct date, time according to your timezone from the internet.
You can get free APIs from TimeZone

Convert Large Switch to Hash (Or Other Recommendation)

I currently have an Android application that displays a schedule for a ferry boat. The application can display the full schedule (just a giant list), but the selling point in the application is it will display when the next two ferries are departing and how long from the current time that departure is.
I am relatively new to Java and currently use large Switch() statements in my code. Basically it gets the current phone time and compares it to all of the times in the schedule at which point it displays the next two departure times and then calculates the difference between current time and the departure times.
I am sure that a switch statement is not the best idea for speed purposes as well as code changing purposes. For example if one time changes its a bunch of lines of code to go in and fix for that one time change. Also if the entire schedule changes everyone has to update their app for the time change to take effect. My ideal situation would be to store a file somewhere on my webserver that could be downloaded and inserted into a hashmap (I think is the correct term) that would load the new schedule if there was a time change.
Not sure how confusing this is, but it would be greatly appreciated if someone could explain how I might use a hashmap or something else you might recommend to get this task accomplished. Currently the variables are the two ferry terminals as well as the day of the week since the schedule changes per day (monday, tues-friday, saturday, sunday).
Below is a screenshot of the application so you can understand it if my post wasn't clear. Thank you in advance.
Screenshot:
Store the schedule objects in a sorted array. You can then binary search the array for the first value greater than the current time. You'll probably use some parent array consisting of the location and applicable day of the week.
You can easily write that kind of data structure to a file that is read & parsed by the application for updates instead of being compiled into the code.
Details of this? First, understand resources in Android. If no updated schedule exists, fall back to the default resource.
Second, use an HTTP head request to check if a newer file exists. If it does, parse, download & save state. Saving Android Activity state using Save Instance State.
Finally, XML is handy for data distribution, even if it's not fast. Everybody understands it and it's easy to update or hand off.
<ferry location=0 time=2045>
<day>1</day>
<day>2</day>
<day>3</day>
<day>4</day>
<day>5</day>
</ferry>
<ferry location=0 time=0800>
<day>6</day>
</ferry>
You will need something like a database to hold the schedule data. That will help you to seperate code from data. I'm not familiar with Android but i think there is a interface to sqlite database on the device.
Further, as this is an application on a small device you may connect to the schedule database on a server thru the internet connection. That way you have to maintain schedule data only in one place (on the server) and clients will use always up to date data.

Getting timezone offset with Joda Time

I got hopelessly stuck on this task. I get other-than-UTC future date input from user > I need to persist it as UTC time. I tried various ways, but it always ends up like this: (method names are irrelevant)
Could please anybody give me the right direction ?
It looks like you're already doing the right thing in the first line. With slight modification:
DateTime instant = getDeadLine(orderBean, localTz);
DateTime.getMillis() will give you the number of milliseconds since the UTC epoch... so that's what you need to persist. If you need to be able to convert back to local time, you'll need to know which time zone to convert back to of course - either using the same one every time, or storing it along with the UTC millis.
One thing to note is that local dates/times aren't always unambiguous - the same local date/time can occur twice due to daylight saving transitions. You'll need to think about whether that will ever be relevant to you.

Categories

Resources