Reputation: 33
I'm trying to collect the minute of the hour. This seems to work in this class. Now I want to use intTime in an other class for some calculations. How do I return intTime. I tried to use the same principles when returning a attribute of an instance, but time is not related to any of the objects I use. Is getIntTime viable?
import java.text.SimpleDateFormat;
import java.util.*;
public class Time extends Database{
public Time(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat ("HH:mm:ss");
String stringTime = sdf.format (cal.getTime());
int intTime = 0;
stringTime = stringTime.substring(3,5); // retrieve the minutes (is recorded as string)
intTime = Integer.parseInt(stringTime);
}
public String getStringTime() {
return intTime;
}
public static void main (String[] args) {
}
}
Upvotes: 1
Views: 174
Reputation: 338266
ZonedDateTime.now( ZoneId.of( "America/Montreal" ) )
.get( ChronoUnit.MINUTE_OF_HOUR )
The Answer by Chenchuk is correct and should be accepted.
Some other issues covered here.
You can return the minute-of-hour as an int
primitive or Integer
object rather than as a String as seen in the Question.
By the way, avoid an ambiguous name like "Time". If you mean minute-of-hour, say so.
public int getMinuteOfHour() {
int m = Integer.parseInt( yourStringGoesHere ) ;
return m ;
}
All of this is unnecessary. You are using the troublesome old legacy date-time classes now supplanted by the java.time classes. The java.time classes already provide your functionality.
Your code ignores crucial issue of time zone. If omitted, your JVM’s current default time zone is implicitly applied. Better to be specific.
We define a time zone as a ZoneId
. Use that to get a ZonedDateTime
as the current moment. Interrogate for the minute-of-hour.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( z );
int minuteOfHour = zdt.get( ChronoUnit.MINUTE_OF_HOUR );
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 0
Reputation: 5742
You need to define the intTime as a class member. In your code, the intTime is 'living' only inside the constructor.
import java.text.SimpleDateFormat;
import java.util.*;
public class Time extends Database{
// class member defined in the class but not inside a method.
private int intTime = 0;
public Time(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat ("HH:mm:ss");
String stringTime = sdf.format (cal.getTime());
// vars defined here, will be gone when method execution is done.
stringTime = stringTime.substring(3,5); // retrieve the minutes (is recorded as string)
// setting the intTime of the instance. it will be available even when method execution is done.
intTime = Integer.parseInt(stringTime);
}
public String getStringTime() {
return intTime;
}
public static void main (String[] args) {
// code here
}
}
Upvotes: 1