Convert part of string to integer - string

I have two time values of type unicode and str like below:
time1 = "10:00 AM" #type: str
time2 = "10:15 AM" #type: unicode
I want to convert integer part of time1 and time2 i.e 10:00 and 10:15 to integer and check if time2 > time1.
Is there any way to convert part of string and unicode to integer ?

public static void main(String[] args) {
String s1 = "10:00 AM";
String s2 = "10:20 AM";
int s1_mins = toMinutes(toNumber(s1));
int s2_mins = toMinutes(toNumber(s2));
if(s1_mins < s2_mins){
System.out.println(s2 +" is more than "+ s1);
}else{
System.out.println(s1 +" is more than "+s2);
}
}
private static String toNumber(String s) {
String[] timeInNumber = s.split(" ");
return timeInNumber[0];
}
private static int toMinutes(String s) {
String[] hourMin = s.split(":");
int hour = Integer.parseInt(hourMin[0]);
int mins = Integer.parseInt(hourMin[1]);
int hoursInMins = hour * 60;
return hoursInMins + mins;
}
The above code helps you.

You should use datetime or time instead which provides an option to compare datetime objects:
from datetime import datetime
time_obj1 = datetime.strptime(time1, '%I:%M %p')
time_obj2 = datetime.strptime(time2, '%I:%M %p')
if time_obj1 > time_obj2:
...

Related

scope calling user-defined C# methods in #DECLARE statement

Is it possible to use a user-defined C# method in a #DECLARE statement? I have some code like this, but it doesn't seem to work.
#DECLARE timespan int = 120;
#DECLARE endDate string = #"2022-03-09";
#DECLARE startDate string = GetStartDate(#endDate, #timespan);
...
#CS
public static string GetStartDate(string endDateString, int daysBefore)
{
TimeSpan timespan = new TimeSpan(daysBefore, 0, 0, 0);
DateTime endDate = DateTime.Parse(endDateString);
DateTime startDate = endDate - timespan;
var startDateString = startDate.ToString("u", CultureInfo.GetCultureInfo("en-US"));
startDateString = startDateString.Split(' ')[0];
return startDateString;
}
#ENDCS

How to format int64 to upper case hex in Vala?

The following example does not compile
public static int main (string[] args) {
var now = new GLib.DateTime.now_utc();
int64 val = now.to_unix();
print ("%" + int64.FORMAT + "\n", val);
print ("%X\n", val); // ERROR
return 0;
}
There is a int64 format string for decimal representation but none for hex (See Valadoc). The %X does also not work. How do I get upper case hex formatting for a int64?
The error is a type error: Argument 2: Cannot convert from int64 to uint
print uses the printf format string and accepts the long long type, which is specified as at least 64 bits. You can use %llX to print out an int64 as upper case hexadecimal.
Working example:
void main () {
var now = new GLib.DateTime.now_utc();
int64 val = now.to_unix();
print ("%" + int64.FORMAT + "\n", val);
print ("%llX\n", val);
}

How can I make this more elegant and applicable to any String?

public class JavaIntern{
public static void main(String []args){
String str = "JavaIntern"; //hardcoded, but not the problem
char[] s = str.toCharArray();
String result = new String (s,0,1); //this is where the dilemma begins
System.out.println(result);
String result1 = new String (s,0,2);
System.out.println(result1);
String result2 = new String (s,0,3);
System.out.println(result2);
String result3 = new String (s,0,4);
System.out.println(result3);
String result4 = new String (s,0,5);
System.out.println(result4);
String result5 = new String (s,0,6);
System.out.println(result5);
String result6 = new String (s,0,7);
System.out.println(result6);
String result7 = new String (s,0,8);
System.out.println(result7);
String result8 = new String (s,0,9);
System.out.println(result8);
String result9 = new String (s,0,10);
System.out.println(result9); //and this is where it ends... how can I get rid of this?
}
}
//but still get this:
J
Ja
Jav
Java
JavaI
JavaIn
JavaInt
JavaInte
JavaInter
JavaIntern
I guess you want to improve the code and also don't depend on the length of the string.
What about something like this?
public class JavaIntern{
public static void main(String []args){
String str = "JavaIntern"; //hardcoded, but not the problem
String substring = "";
for (char ch: str.toCharArray()) {
substring += ch;
System.out.println(substring);
}
}
}
This will also print:
J
Ja
Jav
Java
JavaI
JavaIn
JavaInt
JavaInte
JavaInter
JavaIntern
The loop gets one character of the string at a time and concatenates it to the substring before printing it.
Im assuming you want to be able to print out one letter more each time.
To do this we use a for loop, and this way it is fairly simple.
public class MainClass {
public static void main(String[] args) {
String str = "JavaIntern";
for (int i = 1; i <= str.length(); i++) {
System.out.println(str.substring(0, i));
}
}
}
We set i to 0 in the loop, keep iterating while i less than or equal to the length of the string, and each time we iterate, add one to i.
We use the substring method to split the string from the first letter, to i.

Map value is not getting increased when it finds duplicate key. Please advise

I am new to Java.. Please help me to get required output for the below code.
Map value has to be counted when month value is repeated
Issue occurs due to 2 sublist which is executing on a different threads. Please advise if there is any modification needs to be done in this code in order to rectify map value to get counted.
int size = inputList.size();
int listSize = size/numberOfThreads;
List<String> tmplist1 = inputList.subList(0, listSize);
int count = listSize;
for (int i=0;i<numberOfThreads;i++)
{
if(listSize <= size) {
t1 = new TotalOrderThread();
t1.setInput(tmplist1);
Thread thread = new Thread(t1);
thread.start();
thread.join();
if(listSize < size)
tmplist1 = inputList.subList(listSize, listSize+count);
listSize=listSize+count;
}
*
Input:
123,03/04/2005
234,04/05/2005
567,03/04/2005
789,01/01/2005
Output:(month 4 is repeated twice but the value is not getting counted as 2). Please help to find out the mistake. Also, Is there anyway to print the month value as "MMM" format while iterating map?
4 1
5 1
1 1
4 1
*
Map<Integer,Integer> orderMap = new HashMap<>();
for(int i=0;i<this.input.size();i++)
{
String details = input.get(i);
String[] detailsarr = details.split(",");
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate id = LocalDate.parse(detailsarr[1], f);
int month = id.getMonthValue();
if(orderMap.containsKey(month))
{
int count = orderMap.get(month);
orderMap.put(month, count+1);
}
else
{
orderMap.put(month, 1);
}
}
for(Map.Entry<Integer,Integer> entry : orderMap.entrySet())
{
int month3 = entry.getKey();
int value = entry.getValue();
System.out.println(month3+ " " +value);
}
}
I tried your code like below. This one works.
Map<Integer,Integer> orderMap = new HashMap<>();
String[] input = {"123,03/04/2005", "234,04/05/2005", "567,03/04/2005", "789,01/01/2005"};
for(int i=0;i<input.length;i++)
{
String details = input[i];
String[] detailsarr = details.split(",");
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate id = LocalDate.parse(detailsarr[1], f);
int month = id.getMonthValue();
if(orderMap.containsKey(month))
{
int count = orderMap.get(month);
orderMap.put(month, count+1);
}
else
{
orderMap.put(month, 1);
}
}
for(Map.Entry<Integer,Integer> entry : orderMap.entrySet())
{
int month3 = entry.getKey();
int value = entry.getValue();
System.out.println(month3+ " " +value);
}

How to format a Date variable in LWUIT?

I have an object whose class has a getter method , and this getter method returns a Date value. I want to show this value in a Label in the format DD/MM/YYYY.
How to achieve that with LWUIT ?
Thank you very much indeed
You can use this code to convert date to string format and pass the this string value to label.
public static String dateToString (long date)
{
Calendar c = Calendar.getInstance();
c.setTime(new Date(date));
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH) + 1;
int d = c.get(Calendar.DATE);
String t = (d<10? "0": "")+d+"/"+(m<10? "0": "")+m+"/"+(y<10? "0": "")+y;
return t;
}
To thank you here is a code of mine which formats a number :
public static String formatNombre(int trivialNombre, String separateur)
{
String pNombre, sNombreLeads, sNombre, argNombre, resultat;
int leadingBits;
int nbBit;
pNombre = String.valueOf(trivialNombre);
if (pNombre.length() > 3)
{
leadingBits = (pNombre.length())%3;
if (leadingBits != 0)
sNombreLeads = pNombre.substring(0, leadingBits).concat(separateur);
else
sNombreLeads = "";
nbBit = 0;
sNombre = "";
argNombre = pNombre.substring(leadingBits);
for (int i=0;i<argNombre.length();i++)
{
sNombre = sNombre.concat(String.valueOf(argNombre.charAt(i)));
nbBit++;
if (nbBit%3 == 0)
sNombre = sNombre.concat(separateur);
}
sNombre = sNombre.substring(0, sNombre.length() - 1);
resultat = sNombreLeads.concat(sNombre);
return resultat;
}
else
return pNombre;
}

Resources