Converting Military Time To Standard Time?

Jan 29, 2014

I have two classes. time_runner is used for testing my code.

This is what I'm using to test my code:
 
class time_runner
{
 public static void main(String str[]) throws IOException {
 Time time1 = new Time(14, 56);
System.out.println("time1: " + time1);
System.out.println("convert time1 to standard time: " + time1.convert());
System.out.println("time1: " + time1);
System.out.print("increment time1 five times: ");
time1.increment();

[code]....

The two constructors are "Time()", which is the default constructor that sets the time to 1200, and "Time(int h, int m)" Which says If h is between 1 and 23 inclusive, set the hour to h. Otherwise, set the hour to 0. If m is between 0 and 59 inclusive, set the minutes to m. Otherwise, set the minutes to 0. Those are my two constructors that I pretty much have down. The three methods however I'm having trouble with. The "String toString()" Returns the time as a String of length 4. The "String convert()" Returns the time as a String converted from military time to standard time. The "void increment()" Advances the time by one minute.

public class Time {
private int hour;
private int minute;
  public Time(int h, int m) {
if(h > 1 && h < 23)
hour = h;

[code]....

View Replies


ADVERTISEMENT

Military Time - Adding Minutes Displaying Correct Time

Feb 9, 2015

I am working on an assignment that I can't seem to figure out the final part to. The program takes in course data such as the time the class starts and how long it lasts. The time is in military time (0000 - 2400)

I need the output time to be the time the class started, plus the length of the class, and displayed in military time.

for example,

Start Time = 0930
Length = 50 minutes
Endtime = 1020

I can't for the life of me figure out how to do this. I have gotten a program that works for this time and minutes, and displays the correct 1020. But when I change the information to say

Start time: 0700
Length = 90 minutes

I get:

Endtime = 90

90 is technically correct, the way the formula is setup, but I need it to display 0900 not 90.

Here is the code that I have. Be easy, I'm still learning, and this is just the file I created to get the formula to work. Also, the verbose in here is just for my own debugging to make sure values should be what I'm expecting them to be.

public class calc
{
public static void main(String[] args) {
double hours, minutes, length;
double temp;
int time = 2400;
hours = time / 100;
System.out.println("Hours are: " + hours);

[Code] ....

View Replies View Related

Elapsed Time Calculator - Output In Military Format

Feb 12, 2014

I am new to java and I have this assignment that I started but I am now stuck . The instructions are:

Create an ElapsedTimeCalculator class that computes the amount of time that passes between a starting time and an ending time, in military format.

For example, suppose the two times are 1445 and 1730. Then the program output should look something like this:

Start time:1445
End time: 1730

Exactly 2 hour(s) and 45 minute(s) have passed.

Total elapsed time in minutes:165
Total elapsed time in hours:2.75

ElapsedTimeCalculator Class Specifications:

1.Your class will have a constructor that takes two parameters: the starting time and the ending time in military format

2.Your class will also have these methods:

-Get method to return the starting and ending times
-Set method that resets the starting and ending times to values passed as parameters
-a method that returns the hour portion of the elapsed time
-a method that returns the minute portion of the elapsed time
-a method that returns the total elapsed time in minutes
-a method that returns the total elapsed time in hours

Now here below you can see what I have so far...

public class ElapsedTimeCalculator {
private int startTime, endTime;
private int hours;
private int minutes;
private int seconds;
//constructor
ElapsedTimeCalculator(int start, int end)

[Code] ....

I don't have any errors so far, I am more stuck on how to begin a method that returns the hour, minute, etc.. (the third bullet).

View Replies View Related

Converting String To Date And Time MYSQL

Apr 8, 2014

In my java file I made Strings of date and time, but my MYSQL database needs Date and Time of course. I've tried to convert them, but I keep getting this exception: org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "2013-02-20"

public class Vogel {
static final String url = "jdbc:mysql://localhost:3306/turving";
public static void Insert(String datum, String tijd, String plaats, String spotternaam, String vogelsoort) {
try {
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat formatt = new SimpleDateFormat("HH:MM:SS");
java.util.Date parsed = format.parse(datum);
java.util.Date parsedd = formatt.parse(tijd);
java.sql.Date sql = new java.sql.Date(parsed.getTime());
java.sql.Time sqll = new java.sql.Time(parsed.getTime());

[code]....

View Replies View Related

Time Comparison - Converting To Seconds / Minutes And Hours

Apr 27, 2013

I want to compare two times. What I am doing is

java.util.Date date = new java.util.Date();
java.sql.Time cur_time = new java.sql.Time(date.getTime());

/*this is my database connectivity code from where I am getting second date that I am comparing. This is right. Don't bother it.

Connection c= ConnectionManager.getConnection();
String sql = "select t from datesheet where subjectCode=? and sessional=? and d=?";
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1,subjectCode);
ps.setString(2,sessional);
ps.setDate(3,cur_date);
ResultSet rs = ps.executeQuery();*/
if(rs.next())
{
java.sql.Time t = rs.getTime("t");
long diff = cur_time.getTime()-t.getTime();
}

Then I am converting it to seconds, minutes and hours, but the problem is time that we get from cur_time.getTime() has more digits than time that we get from t.getTime(), so it is always greater than t.getTime() even when it is not.

long seconds = (diff/1000)%60;
long minutes = (diff/60000)%60;
long hours = (diff/(60*60*1000))%24;

View Replies View Related

How To Run Two Methods At The Same Time

Jun 22, 2014

Below is the main class of a project ive been working on, the goal is to start a countdown specified by the user. When the countdown reaches zero the base drops in the song that is being played. (Its not done yet) The main problem that arises is the fact that my song plays, and AFTER that, the timer starts.

Output:

Please input countdown in HH:mm:ss format.
00:00:41
Start?
Yes

The name of of the song is: Skrillex & Damian "Jr Gong" Marley - "Make It Bun Dem"

The time of base drop is: 00:00:41 //Song starts here

//Song is done
//Then timer starts
00:00:41
00:00:40
00:00:39

[code].....

View Replies View Related

How To Set Time For Calendar

Mar 7, 2014

I would like to set a specific time for a Calendar instance. My below code will set a time one minute ahead of the current time, and format it to ISO8601 standard.

Java Code:

Calendar reminderDate = Calendar.getInstance();
System.out.println(dateFormat.format(reminderDate.getTime()));
reminderDate.add(Calendar.MINUTE,1);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String finalReminderDate = sdf1.format(reminderDate.getTime()); mh_sh_highlight_all('java');

I now want to set another variable(deadlineDate), but to a specific time, say 5 minutes ahead. I would like to hardcode this in so I dont want it to change as a result of getting the instance of the Calendar(which sets it to the current time)

Essentially I am trying to regenerate the above code every minute and increment itself each cycle until it reaches the deadlineDate, which is a fixed datetime.

How can I do this?

View Replies View Related

How To Iterate Two Lists At A Time

Mar 25, 2015

List list1 = query1.list();
List list2 = query2.list();
 
for (Iterator itr = list1.iterator(); itr.hasNext();) {
Object[] row = (Object[]) itr.next();
}

View Replies View Related

Going Through All The Keys On Keyboard One At A Time

Jul 14, 2014

I am writing a code that tries to figure out the users password by going through every possible key (brute force). Although I think its going to work, it looks EXTREMELY inefficient to me, since its just a huge switch statement for each character -- 94 total, including the shift values. Is there a built in method in the JAVA API that goes through every key or something?

Here is my code for that segment:

public char[] HackingPassword(){
char[] passChars = this.password.toCharArray();//convert the password to char[]
char[] hacking = new char[passChars.length];//make the hacking variable same size as users password
int nextCharValue = 0;//this is used to cycle through the keyboard
//check each letter of hacking to match with password
for(int i = 0; i < passChars.length; i++){

[Code] .....

View Replies View Related

How To Reduce Time In Java

Jul 3, 2014

I have two string representations of time in "HH:MM:SS" and I want to minus one time from the other.

For example if a = 10:20:30 and b = 00:50:20, i would get 09:30:10. What is the best way to do this?

View Replies View Related

JPanel Is Not Displaying All The Time

Jul 2, 2014

Yes my JPanel is not displaying all the time like you would think it would. Before you beat me up yes I am a bit new I am just working on the GUI part of java over my summer break. Obviously it is not suppose to do that. Secondly, As you can see i am using the alphabet to create JButtons. Shouldnt there be an easier way to use a for( loop) . I tried before but I didn't want to get too side tracked.

Java Code:

import javax.swing.*;
import java.awt.*;
public class hello {
private JFrame alphaframe1;
private JFrame alphaframe2;

[Code] .....

View Replies View Related

String To Calendar Time

Feb 9, 2014

I'm having an issue with this little bit of conversion. I'm converting a string (_dateString) into a Calendar time. I am using DateFormat and SimpleDateFormat to accomplish this task. Everything seems to be working great, except for it figuring out whether it is AM or PM. According to SimpleDateFormat (Java Platform SE 7 ) I am using "aa" to get my AM or PM marker. How come in my output then, it believes it to be 4:45 am instead of 4:45 pm? Hour of Day should return the 24 hour clock, which should show 16 instead of 4. I have posted the output below my code.

Java Code:

import java.text.*;
import java.util.*;
public class Time{
static String _dateString = "08 Feb 2014, 4:45pm";
public static void main(String args[]){
Calendar cal=Calendar.getInstance();

[Code] ....

Output:

Java Code:

Today is Sat Feb 08 04:45:00 EST 2014
Year: 2014
Month: 1
Day of Month: 8
Day of Week: 7
Week Of Year: 6
Week of Month: 2
Hour: 4
Hour of Day: 4
Minute: 45
Second: 0
Millisecond: 0 mh_sh_highlight_all('java');

View Replies View Related

Getting User Input Second Time

Apr 19, 2014

I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.

//Create Scanner object
Scanner keys = new Scanner(System.in);
//Get chips
System.out.print("How much money would you like to change? ");
int chips = keys.nextInt();
System.out.println("You now have $" + chips + " in chips.");

[code]...

This is what I get when I run it run/How much money would you like to change?

View Replies View Related

Java Addition Of Time

Jun 28, 2014

I work as a golf staff I would like to create application which would tell me the playing time and the time when players need to reach certain playing field/hole. my ideas is to make a program which would ask the user to input their starting time and than select the hole number, where the end result would be amount of actuall time.

I have been having hard time to figure out howe to properly structure the input conversion so it is recognized as a time (Exampke: 10:15). Do I need to use the calendar method in Java or ?

View Replies View Related

How To Get Time Shown In JFrame

Aug 8, 2014

I am trying to to insert time onto my jFrame (Netbeans) by using textfields. this is the code i've tried to put into the textfield so whenever i run the program it automatically shows the time.

Calendar now = Calendar.getInstance();
hours = now.get(Calendar.HOUR);
minutes = now.get(Calendar.MINUTE);
seconds = now.get(Calendar.SECOND);
millis = now.get(Calendar.MILLISECOND);

[Code] ....

View Replies View Related

One Time Password Generation

Jan 21, 2014

I Have one query regarding OTP, In my project while registration the otp should be generate and send to the mobile number of user which is registered the form.

View Replies View Related

Print Current Time Every Second?

Nov 13, 2014

I need to write a program that prints the current time every second. Here is what I have and I am not entirely sure why I'm not.

Java Code:

import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class CurrentTime

[Code] .....

View Replies View Related

How To Find The Best Time Of 14 Run Times

Oct 19, 2014

I have this code that prints the run time for each for loop iteration but how do I find the fastest time store it in a variable and the print that variable at the end? This is what I have currently.

for (int i = 1; i <= 14; i++) {
int n = (int) Math.pow(2, i);
int[] list = new int[n];
for (int j = 0; j < list.length; j++) {
list[j] = (int) (Math.random() * 2000);
}
int length = list.length;
double radixTime = radixSort(list, length);
System.out.println("For base " + i + " the time was " + radixTime);
}

View Replies View Related

Get Current Utc Date / Time?

Jun 2, 2014

I am trying to get the current utc date/time, but it keeps printing out in local time:

Java Code: LocalDateTime dt = LocalDateTime.now();
ZonedDateTime zdt = dt.atZone(ZoneOffset.UTC);
System.out.println("time: " + zdt);
// output:
time: 2014-06-02T13:37:55.705Z mh_sh_highlight_all('java');
13:37 is local time. Utc time is 17:37.

View Replies View Related

Time Input Validation

Nov 24, 2014

I am trying to validate user input of time. It seems that only part of it works. It will only accept hour 20-23 as valid.

void timeValidate() {
String value = "";
boolean bTryAgain = true;
Scanner sc = new Scanner(System.in);

[Code] .....

View Replies View Related

LocalDateTime Class And UTC Time

Jun 8, 2014

Does LocalDateTime.now() return the date in UTC time or local time?When I check with:

System.out.println("current time: " + LocalDateTime.now());

It gives local time. However, I am not sure if it is the toString() method of LocalDateTime that converts it to local time, or if it indeed returns time in local time. I need to save data to database in utc time, and I would like to know if LocalDateTime can give utc time.

View Replies View Related

How To Get System Time In Java

Jun 1, 2014

I just want to calculate search time for my algorithm . How to get system time in java other than System.nanotime() and System.currenttimemillis() as these methods does not returns consistent time for same input is their another option to get system time???

View Replies View Related

Joda Time Threading

Nov 20, 2014

I have an application that uses multiple threads to access logic. I am using joda time to compare dates because joda time is thread safe. Below is the example method I am using. My question is being that joda time is thread safe, does it matter whether or not I am using a static method rather than an instance method?

Java Code:

public static boolean isDateEqual(Date checkedDate, String targetDateString) {
boolean passed = false;
if (checkedDate != null && targetDateString != null && !targetDateString.equals("")) {
DateTime checkedDateTime = new DateTime(checkedDate);
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTime targetDateTime = fmt.parseDateTime(targetDateString);
if (DateTimeComparator.getDateOnlyInstance().compare(checkedDateTime, targetDateTime) == 0) {
passed = true;
}
}
return passed;
} mh_sh_highlight_all('java');

View Replies View Related

Time Management System

Oct 27, 2014

I'm in the middle of building A time management system for myself.I'm currently using JodaTime for timing in java. I just can't figure out who to save my data like, the starttime, stoptime and day time.

public void save() throws SQLException {
String checkSql = "select count(*) as count from Time where id=?";
PreparedStatement checkStmt = con.prepareStatement(checkSql);
String insertSql = "insert into Time (id, day, starttime, stoptime, overtime, exception, hours) values (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement insertStatement = con.prepareStatement(insertSql);

String updateSql = "update Time set day=?, starttime=?, stoptime=?, overtime=?, exception=?, hours=? where id=?";
PreparedStatement updateStatement = con.prepareStatement(updateSql);

[code]....

View Replies View Related

Incrementing Count By 1 Each Time

May 25, 2014

I am currently take a class, and trying to increment the count of employees I enter into an ArrayList by 1 for every entry. However, instead of incrementing by 1 each time, the current number of employeeIDs adds itself to itself, then adds 1, giving a non-sequential number (i.e. an employeeID of 75 will add itself to 75 + 1 to get a new employeeID of 151).

The following code is what I have done in terms of incrementing. (More coding has been done, but it is not used to increment, so I have omitted it.

public class Employee
{
// instance variables
private String firstName;
private String lastName;
private int employeeID;
static int newEmployeeID = 0;

[Code] ....

View Replies View Related

What Is A Compile Time Constant

Jul 16, 2009

"Because X is a compile time constant, the compiler will Y"... But what exactly is a compile time constant? And how can we determine whether something is treated as such?Obviously, a compile time constant is a constant value that is known at compile time... ... Literals are, by definition, compile time constants -- as they are constants, known at compile time.

But the definition of a compile time constant is a bit more complex. To start, let's examine section 15.28 of the Java language specification.A compile-time constant expression is an expression denoting a value of primitive type or a String that is composed using only the following:

Literals of primitive type and literals of type String Casts to primitive types and casts to type StringThe unary operators +, -, ~, and ! (but not ++ or --)The multiplicative operators *, /, and %The additive operators + and - The shift operators <<, >>, and >>>The relational operators <, <=, >, and >= (but not instanceof)The equality operators == and !=The bitwise and logical operators &, ^, and |The conditional-and operator && and the conditional-or operator ||The ternary conditional operator ? : Simple names that refer to final variables whose initializers are constant expressions Qualified names of the form TypeName . Identifier that refer to final variables whose initializers are constant expressions

This is the full definition of a compile time constant. And as you can see, it contains more than just literals. In fact, literals are merely the first bullet point on the list. Also, note that a compile time constant can apply to any literal that is of primative or String type.The next few bullet points are the operations that can be applied to a constant at compile time. This list is actually pretty long, as it is possible to apply most of the operations at compile time. It may actually be easier to remember what can't be apply at compile time -- pre and post increment and decrement, instanceof operator, or any method calls, are not on the list.

The last few bullets are the most interesting. It is possible to use a variable in the expression -- provided that the variable is a compile time constant variable. So... what is a constant variable? Going back to the JLS (section 4.12.4 to be exact)..4.12.4 final Variables.A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).The last part of the definition is the relevant part (I still find it amazing that this is that well hidden in the specification). To be a variable that is a compile time constant, the variable needs to be...declared as finalhave a primative or String typeinitialized (on the same line as the declaration)assigned to a compile time constant expression.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved