Working out time between 7am to 3:30pm - JAVA [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've created this program to calculate the time between startWork and finishWork
but I cant seem to figure out how to calculate time...
This is my Interface.
Just wanting to know a way of approaching this calculation.
Thanks

Use the Duration class from java.time to represent your working time. Let Duration.between() do the calculation for you, passing two LocalTime or two ZonedDateTime objects to it as appropriate. The latter will take transitions to and from summer time (DST) into the calculation if such a transition happens during the working hours.
If the time is entered as for example 1530 or 3:30pm, define a DateTimeFormatter to parse it into LocalTime.
Duration objects can be summed using its plus method, so you can calculate the hourly and monthly working time and so on.
To format the working time into for example 8.5 (for 8 hours 30 minutes), use the toMinutes method, then convert to double before you divide by 60 (I would declare the constant 60 as final double minutesPerHour = TimeUnit.HOURS.toMinutes(1);).
java.time
java.time is the modern Java date and time API. It came out nearly 4 years ago to replace the outdated and poorly designed date and time classes from Java 1.0 and 1.1 from the last years of the previous millennium.
Link: Oracle Tutorial trail Date Time

Use java.time as suggested by Ole V.V.:
String time1 = "07:00:00";
String time2 = "15:30:12";
LocalTime t1 = LocalTime.parse(time1);
LocalTime t2 = LocalTime.parse(time2);
Duration diff = Duration.between(t1, t2);
System.out.println(diff.toString());
Prints:
PT8H30M12S

Related

Read a time from file in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am doing a work for the university and I have to read many lines from file a with that format:
3ld4R7 4:27 3475
Everything is correct, each line represents a song, the first string is the name, the second the duration and the third the popularity. However, I don't know exactly what type I can choose for the time. Then, I have to do many operations with the time (minutes, seconds, hours). I don't know if there is a class in Java libraries for that such as Time or something like that. Any help is thanked!!!
java.time.Duration
The Duration class of java.time, the modern Java date and time API, is the class for — well, the name says it already. Unfortunately parsing a string like 4:27 into a Duration is not built-in. My preferred trick is:
String durationString = "4:27";
String isoString = durationString.replaceFirst("^(\\d+):(\\d+)$", "PT$1M$2S");
Duration dur = Duration.parse(isoString);
System.out.println(dur);
Output:
PT4M27S
Read as a period of time of 4 minutes 27 seconds. The Duration.parse method requires a format known as ISO 8601, an international standard. And Duration.toString(), implicitly called when we print the Duration, produces ISO 8601 back. It goes like what you saw, PT4M27S. So in my code, the first thing I do is convert your input from the file to ISO 8601 format, which I then parse.
If you want to format the duration for display, for example back in the same format as in the file:
System.out.format("%d:%02d%n", dur.toMinutes(), dur.toSecondsPart());
4:27
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601
There is such a library in Java and more than one.
Try java.util.Date library and SimeplDateFormatter class to parse the date-time objects in a specific way according to the strings.
For example:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy h:mm a");
Date date = (Date)formatter.parse(date)

What (if anything) is the significance of "2007-12-03T10:15:30.00Z" in the Java date/time parse examples? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I've noticed that the various Java time parse methods (such as ZonedDateTime.parse(...)) consistently use the relevant portions of 2007-12-03T10:15:30+01:00[Europe/Paris] as the example in their Javadocs (with the exception of Instant which uses UTC as the time zone).
Obtains an instance of ZonedDateTime from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris].
Class
Example
Instant
2007-12-03T10:15:30.00Z
LocalDate
2007-12-03
LocalDateTime
2007-12-03T10:15:30
LocalTime
10:15
MonthDay
--12-03
OffsetDateTime
2007-12-03T10:15:30+01:00
OffsetTime
10:15:30+01:00
Year
2007
YearMonth
2007-12
ZonedDateTime
2007-12-03T10:15:30+01:00[Europe/Paris]
I realize this might just be an arbitrary date, but I've found in the past that oftentime the example values have additional meaning that help my understanding of the overall domain, beyond being just an example.
Is there any particular significance to this datetime, and why was it chosen as the example parse value for the Java time API?
I'm looking specifically for something that can be backed up with something concrete (e.g. official implementation discussions or statements by those involved in the library creation).
No special meaning
No, there is no special meaning to that example date-time value. Date-time handling is tricky enough, do not distract yourself with such trivial detail.
Technical writers commonly work with the same example data across scenarios for consistency, to most easily make apparent the similarities and contrasts.
The value may have personal significance to the original author. But as Arvind Kumar Avinash commented, what matters here is the formats rather than the value.
2007-12-03T10:15:30.00Z is not really an ideal example. I would have chosen a day-of-month larger than 12 to distinguish from the month number. And I would have chosen an hour larger than 12 to make obvious the 24-hour clock (0-23).

How do I calculate actions done per minute with Java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm writting a own application to help RuneScape 3 streamers educate their viewers with a DPS Rotation showing, and i'm wanting to implement a feature A.K.A APM (Which means Actions per minute) which is in other words, number of Keys (hotkeys) pressed within a minute, what i'm doing atm is, having a LocalTime when the thread starts, and every 5 secs it should show APM, but I guess my formula isn't correct.
Current formula is:
LocalTime apm = Main.keysPressed.size() / (LocalTime.now().minus(Main.apmLocalTime);
Which is:
counter / (current_time - start_time)
Problem is, the .minus() asks for a TemporalUnit as parameter, and i'm quite lost.
Can someone plz help me getting the formula.
tl;dr
actionsCount / Duration.between( start , Instant.now() ).toMinutes()
java.time.Instant
Use Instant to track a moment, not LocalTime.
Instant represents a moment, a point on the timeline, as seen in UTC. The class resolves to nanoseconds, but current conventional hardware clocks limit capturing the current moment to microseconds or milliseconds.
The LocalTime class represents merely a time-of-day without the context of a date and time zone or offset-from-UTC. So this class cannot represent a moment.
Use Duration class for ease, and to make your code more self-documenting.
Instant start = Instant.now() ;
…
Duration elapsed = Duration.between( start , Instant.now() ) ;
long minutesElapsed = elapsed.toMinutes() ; // Get a count of whole minutes in total span of time.
long actionsPerMinute = ( actionsCount / minutesElapsed ) ;
Tip: While generally in Java we want to use the more general interfaces and superclasses rather than the more specific concrete classes, in java.time the opposite is true. In java.time we want to use the specific concrete classes, because the framework programmers told us so in the documentation. So if your IDE or compiler suggests a Temporal, for example, follow the Javadoc to see the list of implementing classes, such as Instant.

Read date from webpage to Selenium java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on Selenium Java, I need to get the following date format without the time, as a string in selenium java to validate whether it is up to date with the published date. I used getText() method from the website by splitting from the time and date. Is there any other best ways rather than this solution!
java.time
Edit: I have added more explanation and more code lines.
There’s a little challenge in the fact that the string on the website does not include year. One simple way to handle it is:
ZoneId websiteTimeZone = ZoneId.of("America/Lower_Princes");
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("dd-MMM HH:mm", Locale.ENGLISH);
String stringFromWebsite = "06-Feb 06:37";
MonthDay today = MonthDay.now(websiteTimeZone);
System.out.println("Today is " + today);
MonthDay date = MonthDay.parse(stringFromWebsite, formatter);
System.out.println("Date from website is " + date);
if (date.equals(today)) {
System.out.println("It’s up to date");
} else {
System.out.println("It’s *NOT* up to date");
}
When I ran today (March 12), the snippet printed:
Today is --03-12
Date from website is --02-06
It’s *NOT* up to date
A MonthDay is a month and day of month without year. The advantage of using this class is we don’t need concern ourselves with year. A possible drawback is we can’t compare two such objects determine which one is before or after the other one. Such a comparison would require knowing the year of each one.
We need to know the time zone that the website uses since it is never the same date everywhere on Earth. Please insert the correct one where I put America/Lower_Princes.
I am parsing the string from the website into a MonthDay using a DateTimeFormatter with format pattern dd-MMM HH:mm since lower case d is for day of month, M is for month, H for hour of day and lower case m for minut of the hour. Since I am parsing into a MonthDay, the time is ignored (only its syntax still checked). In the print --03-12 means March 12 and --02-06 similarly February 6 (the date from the website). Since they are not the same, the code prints that the website is not up to date.
A more advanced solution might check if the date is a few days before or after today’s date and/or also look at the time.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Stack Overflow question How do I simply parse a date without a year specified?
You can use selenium's getText(), in order to acquire the value as a String.
Afterwards you can use Java's DateTimeFormatter, to parse this date, and transform it to the format you want

Formula of currentTimeMillis() in java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
To my knowledge, System.currentTimeMillis()/1000 can show the current time in seconds since
1970-1-1 00:00:00 (YY-MM-DD HH:mm:ss)
For example
2013-10-12 21:30:00 (YY-MM-DD HH:mm:ss)
= 13815846XX (not sure whats X for)
I was wondering how to calculate it. Thanks a lot!!!!
System.currentTimeMillis() just returns the number of milliseconds since the Unix epoch (January 1st 1970, midnight UTC), as a long.
Converting that value into a string is normally the job of something like SimpleDateFormat, via Calendar and Date. Alternatively, look at Joda Time for a nicer date/time API.
If you want to start with a date and get the number of milliseconds since the Unix epoch, you'd use Calendar, set the appropriate fields and then use Calendar.getTimeInMillis(). (Or again, use Joda Time.) Be careful about time zone interactions.
You can use Epoch Converter to check your computations.
A value such as 1381584600 is most likely to be a Unix timestamp, which is the number of seconds (not milliseconds) since the Unix epoch - hence the division by 1000 that you mention.
If this doesn't tell you what you need, please ask a more precise question.

Categories

Resources