I have searched and found a lot of different things but none that actually help to get what I am looking for. I have two JComboBoxes in which the user can select different times. For example lets say 8:00, and 17:30. Now I want to be able to display the difference between those times, so in this case it would be 9.5. I want it to be in the 9.5, and not 9:30. But my code is doing 9.3, because it is just converting my string to a double.
Any help would be great
public void displaytotal() {
Object sunBobj, sunEobj, mon, tues, wed, thur, fri, sat;
double totalD;
sunBobj = jComboBox1.getSelectedItem();
sunEobj = jComboBox2.getSelectedItem();
String sunB = sunBobj.toString();
String sunE = sunEobj.toString();
try {
double sundB = Double.parseDouble(sunB.replace(":", "."));
double sundE = Double.parseDouble(sunE.replace(":", "."));
totalD = ((sundE - sundB)) * 24;
String totalS = "" + totalD;
jLabel17.setText(totalS);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
You could split the hh:mm string in hours / minutes:
String[] time = sunB.split(":");
int hours = Integer.parseInt(time[0]);
int minutes = Integer.parseInt(time[1]);
double decimalTime = hours + minutes / 60.0;
Related
I am pulling 2 time values (as strings) from an XML file using xpath, these values (for example) are as follows:
00:07
08:00
00:07 is equal to 7 minutes
08:00 means 8am, with no date associated or needed (that is handled elsewhere)
Each of these values is subject to change in each XML file that i read. What i am attempting to do is as follows:
I need to subtract or add (depending on the situation) the 7mins from the 8am and give me a hh:mm time (eg: 07:53 or 08:07) in a string that i can eventually output to CSV
Next i need to produce 2 additional strings, 1 min before and 1 min after (eg: 07:52 and 07:54 OR 08:06 and 08:08) which also need to be output to CSV
I have tried everything and i can think of in relation to the time interpretation and manipulation to get the minutes subtracted/added to the time and then +/- 1 min from there, but being a complete novice i am totally stuck despite reading and testing as much as i could find. Spent the last 2 days working with Joda Time for the first time but i must be missing something fundamental as i cannot get the desired result with this either.
The question is - how can i achieve this?
Some sample code that gets me reading from the XML and printing the time
FileInputStream file = null;
try {
file = new FileInputStream(new File("Output/XmlConfig.xml"));
} catch (FileNotFoundException ex) {
Logger.getLogger(KATT.class.getName()).log(Level.SEVERE, null, ex);
}
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
Logger.getLogger(KATT.class.getName()).log(Level.SEVERE, null, ex);
}
Document xmlDocument = null;
try {
xmlDocument = builder.parse(file);
} catch (SAXException ex) {
Logger.getLogger(KATT.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(KATT.class.getName()).log(Level.SEVERE, null, ex);
}
XPath xPath = XPathFactory.newInstance().newXPath();
//get In Early rule from XML
String exceptionInEarlyXML = "Root/Response/WSAExceptionRule/#InEarly";
NodeList nodeListInEarly = null;
try {
nodeListInEarly = (NodeList) xPath.compile(exceptionInEarlyXML).evaluate(xmlDocument, XPathConstants.NODESET);
} catch (XPathExpressionException ex) {
Logger.getLogger(KATT.class.getName()).log(Level.SEVERE, null, ex);
}
String exceptionInEarly = (nodeListInEarly.item(1).getFirstChild().getNodeValue());
String InEarly = exceptionInEarly;
SimpleDateFormat format = new SimpleDateFormat("hh:mm");
Date d2 = null;
try {
d2 = format.parse(InEarly);
} catch (ParseException ex) {
Logger.getLogger(KATT.class.getName()).log(Level.SEVERE, null, ex);
}
DateTime dt2 = new DateTime(d2);
System.out.println(dt2);
This give me an output of 1970-01-01T00:07:00.000+10:00
I have tried so many permutations of code that i am at the point of deleting and starting again from scratch as it is un-compilable, and i am not experienced enough yet to be able to solve this issue.
Once you have the Date object for the parsed time, use getTime() to get the time in milliseconds and save it into a long variables. Then parse the offset time format and use a NumberFormat to get the number of minutes to offset. Add or subtract as needed. Take the result and create a new Date(millis) then apply your format to it.
Here is a working example:
String sTime = "08:00";
String sOffset ="00:07";
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm");
Date dtTime = null;
try {
dtTime = dateFormat.parse(sTime);
} catch (ParseException e) {
// handle exception
return;
}
String[] offsetHrsMins = null;
NumberFormat numberFormat = NumberFormat.getNumberInstance();
long offsetMillis = 0;
try {
offsetHrsMins = sOffset.split(":");
long offsetHrs = (Long) numberFormat.parse(offsetHrsMins[0]);
long offsetMins = (Long) numberFormat.parse(offsetHrsMins[1]);
offsetMillis = 1000 * 60 * ((offsetHrs * 60) + offsetMins);
} catch (ParseException e) {
// handle exception
return;
}
long lTime = dtTime.getTime();
System.out.println("Adding minutes: " + dateFormat.format(new Date(lTime + offsetMillis)));
System.out.println("Subtracting minutes: " + dateFormat.format(new Date(lTime - offsetMillis)));
output:
Adding minutes: 08:07
Subtracting minutes: 07:53
First, you need to use SimpleDateFormat to parse the Date String to a Java.util.Date Object.
Second, After getting the Date Object, you can easily add/substract some time, and get another Date Object.
Last, you can use another SimpleDateFormat object to format the Date Object you got in second step to String.
SimpleDateFormat is very useful in Processing Date Strings. You can refer to the Javadoc in JDK or search some examples by Google.
Try passing the strings into a method aswel as what you are subrtacting by
Then converting them to ints
Then have an if statment that if the subtraction amount is greater that the minets int
then it subtracts 1 from the hours int and sets the new minets int to 60 subtract the subtraction int
Then convert them back to Strings
Here is the code exept for turing it back into a string
public class Main {
static String hours="8";
static String minets="7";
static String minus="17";
public static void main(String[] args) {
Main m = new Main();
m.timechange(hours,minets,minus);
}
void timechange(String hour, String minuet, String subtract){
int h = Integer.parseInt(hour);
int m = Integer.parseInt(minuet);
int s = Integer.parseInt(subtract);
if(s>m){
h-=1;
m=60-s;
}
else{
m-=s;
}
if ((m>9)&&(h>9)) {
System.out.println(h+":"+m);
} else {if ((m<10)&&(h<10)) {
System.out.println("0"+h+":0"+m);
}else {if ((m<10)&&(h>9)) {
System.out.println(h+":0"+m);
}else {if ((m>9)&&(h<10)) {
System.out.println("0"+h+":"+m);
}
}
}
}
}}
I wasnt sure if you wanted the back to String.
Hopeful that answers your question
The same can be done for when the minets reach over 60 if that ever happens.
Here a genuine Joda-Time answer because OP wants Joda-Time (and I also consider that library as superior to java.util.Date, java.text.SimpleDateFormat etc.):
Joda-Time has the big advantage of having several different temporal types. The right type for handling plain wall times is LocalTime. It also defines a method to add minutes.
Your task:
I need to subtract or add (depending on the situation) the 7mins from the 8am and give me a hh:mm time (eg: 07:53 or 08:07) in a string that i can eventually output to CSV
Next i need to produce 2 additional strings, 1 min before and 1 min after (eg: 07:52 and 07:54 OR 08:06 and 08:08) which also need to be output to CSV
The solution (only for part one, the other part is very similar):
LocalTime time = new LocalTime(8, 0); // corresponds to 08:00
LocalTime laterBy8Minutes = time.plusMinutes(7);
LocalTime earlierBy8Minutes = time.minusMinutes(7);
String sLaterBy8Minutes = laterBy8Minutes.toString("HH:mm"); // 08:07
String sEarlierBy8Minutes = earlierBy8Minutes.toString("HH:mm"); // 07:53
One additional note: If you start with another type like java.util.Date and wish to convert it to LocalTime then you can use the constructor
new LocalTime(jdkDate, DateTimeZone.forID("Europe/Moscow")) // example
or for default timezone:
new LocalTime(jdkDate)
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 8 years ago.
Improve this question
I have a table in which I've assigned burst time for each of machines in the form of time type in second for eg: 00:00:03, 00:00:02 etc.
I have a JAVA code that retrieves these burst times from the database and store it in a list and then convert each burst time into "milliseconds" type.
ArrayList<String>list22=new ArrayList<String>();
ResultSet rs = stmt1
.executeQuery("SELECT burst_time FROM virtual_machine WHERE VM_id <= 4");
while (rs.next()) {
list22.add(rs.getString("burst_time"));
}
String tempStamp = list22.get(0);
int i;
for(i=0;i<=list22.size()-1;i++){
System.out.println(list22.get(i));
}
for(String startstamp : list22){
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
java.util.Date d = null;
try
{
d = formatter.parse(startstamp);}
catch (java.text.ParseException e) {
System.err.println("Error: " + e.getMessage());
}
long qtm= d.getTime();
System.out.println(qtm);
}
This gives me the following result:
00:00:03
00:00:02
00:00:02
00:00:03
3000
2000
2000
3000
Now I need to store those milliseconds values in an array bur[] and use it in the program so that the corresponding machines should run for the assigned time which is stored in the array.
And can u please tell me whether I'm going through the right path in case of storing the milliseconds in array and giving it to the machines.
Following solution is nearly identical to the answer of #nikis, but preserves the important timezone setting. Otherwise users will get a surprising experience if this code runs in UK (Europe/London) because in year 1970 there was summer time - resulting in duration longs with one full hour too much:
long[] bur = new long[list22.size()];
for(int i=0; i < list22.size(); i++) {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
// important, but avoid deprecated Etc/GMT-notation
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
try
{
java.util.Date d = formatter.parse(list22.get(i));
long qtm= d.getTime();
bur[i] = qtm;
System.out.println(qtm);
} catch (java.text.ParseException e) {
System.err.println("Error: " + e.getMessage());
}
}
Hereby I have presented a workaround for an unsupported handling of durations in JDK pre 8. The truth is that SimpleDateFormat is designed to parse points in time, but not durations. Therefore it is so important to have a fixed starting point which never changes, hence the choice of UTC time zone and the reference point 1970-01-01T00:00:00,000Z (elapsed milliseconds since UNIX epoch).
JodaTime offers a specialized PeriodFormatter which really yields a org.joda.time.Period. Else it is possible to write your own specialized string parser (by help of substring(), indexOf() etc.) to factor out the integer parts and then to use Integer.valueOf(String) and then to calculate a long using this simple formula: (hour * 3600 + minute * 60 + second) * 1000.
I've modified your code to avoid NPE and also added bur[] array:
ArrayList<String>list22=new ArrayList<String>();
ResultSet rs = stmt1
.executeQuery("SELECT burst_time FROM virtual_machine WHERE VM_id <= 4");
while (rs.next()) {
list22.add(rs.getString("burst_time"));
}
for(int i=0;i<list22.size();i++){
System.out.println(list22.get(i));
}
long[] bur = new long[list22.size()];
for(int i=0;i<list22.size();i++){
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
try
{
java.util.Date d = formatter.parse(list22.get(i));
long qtm= d.getTime();
bur[i] = qtm;
System.out.println(qtm);
} catch (java.text.ParseException e) {
System.err.println("Error: " + e.getMessage());
}
}
Try this:
int bur[] = new int[list22.size()];
for(int i = 0; i < list22.size(); i++) {
String timeStamp = list22.get(i);
String s, m, h, split;
split = timeStamp.split(":");
h = split[0];
m = split[1];
s = split[2];
bur[i] = Integer.parseInt(s) * 1000 + Integer.parseInt(m) * 60000 + Integer.parseInt(h) * 3600000;
}
This solution doesn't use any date objects, since you won't need them in your case, if I'm not totally on the wrong way ;-)
I am creating a simple GUI-based time card. So I already have the implementation (given by a friend) but it was just made in a non-GUI program.
System.out.print("Enter time-in: ");
String strTimein = input.next();
String timeInArr[] = strTimein.split(":");
double dblTimeInHr = Double.parseDouble(timeInArr[0]);
double dblTimeInMin = Double.parseDouble(timeInArr[1]);
double dblTotalTimeIn = dblTimeInHr + (dblTimeInMin/60);
System.out.print("Enter time-out: ");
String strtimeout = input.next();
String timeOutArr[] = strtimeout.split(":");
double dblTimeOutHr = Double.parseDouble(timeOutArr[0]);
double dblTimeOutMin = Double.parseDouble(timeOutArr[1]);
double dblTotalTimeOut = dblTimeOutHr + (dblTimeOutMin/60);
totalHrs = totalHrs + (dblTotalTimeOut - dblTotalTimeIn);
It works actually. But I couldn't get it to work when I apply it now on my GUI-based program. So I have two JTextField, that is where the user will input the time-in and time-out. And another JTextField, total1, that is setEditable(false) where it will display the total hours.
total1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String timeIn = tIn1.getText();
String timeInArr[] = strtimein.split(":");
double dblTimeInHr = Double.parseDouble(timeInArr[0]);
double dblTimeInMin = Double.parseDouble(timeInArr[1]);
double dblTotalTimeIn = dblTimeInHr + (dblTimeInMin/60);
String timeOut = tOut1.getText();
String timeOutArr[] = strtimeout.split(":");
double dblTimeOutHr = Double.parseDouble(timeOutArr[0]);
double dblTimeOutMin = Double.parseDouble(timeOutArr[1]);
double dblTotalTimeOut = dblTimeOutHr + (dblTimeOutMin/60);
totalHours = totalHours + (dblTotalTimeOut - dblTotalTimeIn);
tal1.setText(totalHours);
}
});
The error I'm getting is "cannot find symbol" which points to:
String timeInArr[] = strTimein.split(":");
and
String timeOutArr[] = strTimeOut.split(":");
I know there's something wrong with my code, but I couldn't figure it out. Please help.
strtimein is not declared anywhere, you probably meant to use timeIn.
You have not declared those two strings in your code..
create these two strTimeOut and strTimeIn
Seems you actually wanted to use timeIn and timeOut
Well....
String timeIn = tIn1.getText();
String timeInArr[] = strtimein.split(":");
I guess you want to split the content of the textfield tIn1, so you should not use strtimein but timeIn. strtimein is not declared anywhere and that's what your error message says.
String timeInArr[] = timeIn.split(":");
The same goes for timeOut / strTimeOut below
I have an ArrayList including several number of time-stamps and the aim is finding the difference of the first and the last elements of the ArrayList.
String a = ArrayList.get(0);
String b = ArrayList.get(ArrayList.size()-1);
long diff = b.getTime() - a.getTime();
I also converted the types to int but still it gives me an error The method getTime is undefined for the type String.
Additional info :
I have a class A which includes
String timeStamp = new SimpleDateFormat("ss S").format(new Date());
and there is a class B which has a method private void dialogDuration(String timeStamp)
and dialogueDuration method includes:
String a = timeSt.get(0); // timeSt is an ArrayList which includes all the timeStamps
String b = timeSt.get(timeSt.size()-1); // This method aims finding the difference of the first and the last elements(timestamps) of the ArrayList (in seconds)
long i = Long.parseLong(a);
long j = Long.parseLong(b);
long diff = j.getTime()- i.getTime();
System.out.println("a: " +i);
System.out.println("b: " +j);
And one condition is that the statement(String timeStamp = new SimpleDateFormat("ss S").format(new Date());) wont be changed in class A. And an object of class B is created in class A so that it invokes the dialogueDuration(timeStamp) method and passes the values of time-stamps to class B.
My problem is this subtraction does not work, it gives an error cannot invoke getTime() method on the primitive type long. It gives the same kind of error also for int and String types?
Thanks a lot in advance!
Maybe like this:
SimpleDateFormat dateFormat = new SimpleDateFormat("ss S");
Date firstParsedDate = dateFormat.parse(a);
Date secondParsedDate = dateFormat.parse(b);
long diff = secondParsedDate.getTime() - firstParsedDate.getTime();
Assuming you have Timestamp objects or Date Objects in your ArrayList you could do:
Timestamp a = timeSt.get(0);
Timestamp b = timeSt.get(timeSt.size()-1);
long diff = b.getTime() - a.getTime();
You can calculate the difference with the both following methods(also you can modify the mentioned methods to return difference as 'millisecond', 'day', 'month', etc by adding additional if statement or using switch case):
private Long calculateDifference(String date1, String date2, String value) {
Timestamp date_1 = stringToTimestamp(date1);
Timestamp date_2 = stringToTimestamp(date2);
long milliseconds = date_1.getTime() - date_2.getTime();
if (value.equals("second"))
return milliseconds / 1000;
if (value.equals("minute"))
return milliseconds / 1000 / 60;
if (value.equals("hours"))
return milliseconds / 1000 / 3600;
else
return new Long(999999999);
}
private Timestamp stringToTimestamp(String date) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsedDate = dateFormat.parse(date);
return new Timestamp(parsedDate.getTime());
} catch (Exception e) {
return null;
}
}
For example:
calculateDifference("2021-10-20 10:00:01", "2021-10-20 10:15:01", "minute");
will return '-15'
or
calculateDifference("2021-10-20 12:00:01", "2021-10-20 10:15:01", "minute");
will return '105'
You should make your ArrayList x to an ArrayList<TimeStamp> x. Subsequently, your method get(int) will return an object of type TimeStamp (instead of a type String). On a TimeStamp you are allowed to invoke getTime().
By the way, do you really need java.sql.TimeStamp? Maybe a simple Date or Calendar is easier and more appropriate.
I have a text file input which contains data as below. How can I display data from the text file into specific format?
Monday
Jessy
Walking
20 minutes
Matthew
Run
20 minutes
Karen
Jogging
40 minutes
Jessica
Run
12 minutes
Tuesday
Messia
Walking
10 minutes
Matthew
Run
20 minutes
Pete
Run
10 minutes
Carol
Walking
30 minutes
I want to display data from the text file into this format:
Day Name Type of exercise Time
Monday Jessy Walking 20 minutes
Matthew Run 20 minutes
Karen Jogging 40 minutes
Jessica Run 12 minutes
Tuesday Messia Walking 10 minutes
Matthew Run 20 minutes
Pete Run 10 minutes
Carol Walking 30 minutes
I just threw this together quickly, but what about something like:
static final String[] DAYS =
{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
public class ActivityEvent
{
public int day;
public String name;
public String typeOfExercise;
public String time;
}
public List loadActivities(String filename) throws IOException
{
List activities = new ArrayList();
FileInputStream fis = new FileInputStream(filename);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
int lastDay = -1;
String line;
while ((line = br.readLine()) != null)
{
line = line.trim();
int day;
for (day = DAYS.length - 1; day >= 0; day--)
{
if (line.equals(DAYS[day]))
{
break;
}
}
String name;
if (day < 0)
{
day = lastDay;
if (lastDay < 0)
{
throw new IOException(filename + " must start with day of week");
}
name = line;
}
else
{
name = br.readLine();
if (name == null)
{
throw new IOException(filename + " expected name, reached end of file");
}
}
String type = br.readLine();
if (type == null)
{
throw new IOException(filename + " expected type of exercise, reached end of file");
}
String time = br.readLine();
if (time != null)
{
throw new IOException(filename + " expected time of exercise, reached end of file");
}
ActivityEvent activity = new ActivityEvent();
activity.day = day;
activity.name = name;
activity.typeOfExercise = type;
activity.time = time;
activities.add(activity);
}
return activities;
}
public void printActivities(List activities)
{
StringBuilder str = new StringBuilder("Day\tName\tType of Exercise\tTime\n");
int numActivities = activities.size();
int lastDay = -1;
for (int index = 0; index < numActivities; index++)
{
ActivityEvent activity = (ActivityEvent)activities.get(index);
if (activity.day != lastDay)
{
str.append(DAYS[activity.day]);
}
str.append('\t');
str.append(activity.name);
str.append('\t');
str.append(activity.typeOfExercise);
str.append('\t');
str.append(activity.time);
str.append('\n');
}
System.out.print(str.toString());
}
And then invoke everything for example:
List activities = loadActivities("somefile.txt");
// Do optional sorting, etc. here.
printActivities(activities);
I would have a look at Java's sprintf() function and it's ability to left/right justify data with specified widths.
Regarding parsing the input:
One issue you will have is that each "record" of data (each row, in the ouput) is not a fixed size.
Some are 3-tuples of name,exercise,time, and others are 4-tuples of day,name,exercise,time
That said, assuming the format you've given is really all there is to it, the issue can be worked around.
After reading a line, you could check for a weekday, and if so assume that's the start of a 4-tuple, and read the next 3 lines.
If it is not a weekday, then assume it is a 3-tuple, and only read the next 2 lines.
If there might be "gaps" in the name, type, or time columns in the output as well, and in different combinations, it gets trickier.
You really need your program to have special knowledge about what values are valid in what columns. Eg, that 'Jessica' is not a valid type of exercise, and 'Jogging' is not a valid name.
Regarding formatting the output
Brian's answer is relevant.
It depends on the language you use. Most languages have a printf-equivalent.
The formatting codes of printf allow you to pad with space, etc.
If you are using Perl (might be well-suited to this task), you can use formats