I have used to LocalTime provided by Java 8 for formatting the time from 1-24 format to 1-12 format in the following code.
String localTime = LocalTime.parse("08:59:00", DateTimeFormatter.ofPattern("HH:mm:ss"))
.format(DateTimeFormatter.ofPattern("hh:mm:ss a"));
And now with the following code, I have got 1-12 hours format
LocalTime localTime1 = LocalTime.parse(localTime, DateTimeFormatter.ofPattern("hh:mm:ss a"));
My question is "is there any API methods provided by LocalTime" to get ante or post from the given time. Or how to get with clean APIs, instead of manipulating the strings with slice and cut.
I am not sure about the elegance of this, but if you want to get a boolean representing whether it is AM or PM, you could do:
boolean isPM = localTime.getHour() >= 12;
This variable isPM will be false if the time is AM and true for PM.
is there any API methods provided by LocalTime
The method is get. We need to use it in conjunction with the ChronoField.AMPM_OF_DAY enum constant. It encodes ante meridiem into 0 and post meridiem into 1.
int amPmOf1159 = LocalTime.of(11, 59).get(ChronoField.AMPM_OF_DAY);
System.out.println(amPmOf1159);
int amPmOf1200 = LocalTime.of(12, 0).get(ChronoField.AMPM_OF_DAY);
System.out.println(amPmOf1200);
Output is:
0
1
Related
One of my first posts, so I'll do my best. I've tried searching this, which is how I got this far..but I could use some help converting some time data in the form mm:ss.000 (that's milliseconds at the end) to seconds with a fraction at the end. For example, 2:15.45 should come out to 135.45.
This works:
t <- "02:15.45" (as.numeric(as.POSIXct(strptime(t, format = "%M:%OS"))) - as.numeric(as.POSIXct(strptime("0", format = "%S"))))
But this one, where I'm trying to use a column of my dataframe (originally in character form) does not work:
starttimesFPsnapjumps <- FPsnapjumps$start (as.numeric(as.POSIXct(strptime(starttimesFPsnapjumps, format = "%M:%OS"))) - as.numeric(as.POSIXct(strptime("0", format = "%S"))))
Perhaps it's because my numbers in the column have an extra 0 - they are mm:ss.000. Any thoughts? Thanks in advance.
I'm trying to find a way to convert a long string ID like "T2hR8VAR4tNULoglmIbpAbyvdRi1y02rBX" to a numerical id.
I thought about getting the ASCII value of each number and then adding them up but I don't think that this is a good way as different numbers can have the same result, for example, "ABC" and "BAC" will have the same result
A = 10, B = 20, C = 50,
ABC = 10 + 20 + 50 = 80
BAC = 20 + 10 + 50 = 80
I also thought about getting each letters ASCII code, then set the numbers next to each other for example "ABC"
so ABC = 102050
this method won't work as having a 20 letter String will result in a huge number, so how can I solve this problem? thank you in advance.
You can use the hashCode() function. "id".hashcode(). All objects implement a variance of this function.
From the documentation:
open fun hashCode(): Int
Returns a hash code value for the object. The general contract of hashCode is:
Whenever it is invoked on the same object more than once, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
If two objects are equal according to the equals() method, then calling the hashCode method on each of the two objects must produce the same integer result.
All platform object implements it by default. There is always a possibility for duplicates if you have lots of ids.
If you use a JVM based kotlin environment the hash will be produced by the
String.hashCode() function from the JVM.
If you need to be 100% confident that there are no possible duplicates, and the input Strings can be up to 20 characters long, then you cannot store the IDs in a 64-bit Long. You will have to use BigInteger:
val id = BigInteger(stringId.toByteArray())
At that point, I question whether there is any point in converting the ID to a numerical format. The String itself can be the ID.
I am trying to find the duration between two times with the below code:
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
System.out.println(airTime1);
System.out.println(startTime1);
Minutes difference = ((Minutes.minutesBetween(startTime1,airTime1)));
String differenceS = String.valueOf(difference);
System.out.println(differenceS);
LocalTime remaining1 = formatter.parseLocalTime(differenceS);
System.out.println(remaining1);
airTime1 & startTime1 are both localTime variables. difference should contain the duration between the two times. differenceS is a String representation of difference, as minutes cannot be converted to String.
When I enter times into the variables such as 12:00 & 13:00, the variables are recorded as: 12:00:00.000 & 13:00:00.000, but differenceS received a value of PT-60M, which obviously throws an error. Does anyone know why the minutes difference line could be calculating this value?
Thanks in advance!
The Minutes class of jodatime overwrites the toString() method in a way that returns a String in ISO8601 duration format as mentioned in the JavaDoc. This is exactly what your PT-60M represents. A duration of -60 minutes.
If you just want the raw minutes printed your code could look like this:
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
System.out.println(airTime1);
System.out.println(startTime1);
Minutes difference = Minutes.minutesBetween(startTime1,airTime1);
System.out.println(Math.abs(difference.getMinutes()));
I am working with an API that requires me to pass in numbers as strings. I need to increment the counter on each call.
I am using the following code:
days = days.to_i
days += 1
days = days.to_s
This works, but seems kind of sloppy. Is there a more way to do this in Ruby?
Yes, there is. You can do:
days = days.next
or
days = days.succ
Or, you can use the bang (!) methods:
days.next!
or
days.succ!
i am trying to format a date using Windows GetDateFormat API function:
nResult = GetDateFormat(
localeId, //0x409 for en-US, or LOCALE_USER_DEFAULT if you're not testing
0, //flags
dt, //a SYSTEMTIME structure
"M/d/yyyy", //the format we require
null, //the output buffer to contain string (null for now while we get the length)
0); //the length of the output buffer (zero while we get the length)
Now we pass it a date/time:
SYSTEMTIME dt;
dt.wYear = 1600;
dt.wMonth = 12;
dt.wDay = 31;
In this case nResult returns zero:
The function returns 0 if it does not succeed. To get extended error information, the application can call GetLastError, which can return one of the following error codes:
ERROR_INSUFFICIENT_BUFFER. A supplied buffer size was not large enough, or it was incorrectly set to NULL.
ERROR_INVALID_FLAGS. The values supplied for flags were not valid.
ERROR_INVALID_PARAMETER. Any of the parameter values was invalid.
If, however, i return a date one day later:
SYSTEMTIME dt;
dt.wYear = 1601;
dt.wMonth = 1;
dt.wDay = 1;
Then it works.
What am i doing wrong? How do i format dates?
e.g. the date of the birth of Christ:
12/25/0000
or the date when the universe started:
-10/22/4004 6:00 PM
or the date Caesar died:
-3/15/44
Bonus Reading
Sorting It All Out: GetDateFormat is Gregorian based
GetDateFormatEx function
This is actually a limitation on SystemTime.
...year/month/day/hour/minute/second/milliseconds value since 1 January 1601 00:00:00 UT... to 31 December 30827 23:59:59.999
I spent some time looking up how to get around this limitation, but since GetDateFormat() takes a SystemTime you'll probably have to bite the bullet and write your own format() method.
SYSTEMTIME struct is valid only from year 1601 through 30827, because in Windows machines, is system time counted from elapsed intervals from 1.1.1601 00:00. See
Wikipedia article.