Reputation: 39
import java.util.GregorianCalendar ;
public class MyCalendar extends GregorianCalendar {
public static void main(String[] args) {
MyCalendar a = new MyCalendar();
System.out.println(a.getCurrenttime());
}
public String getCurrenttime(){
String time= YEAR+"-"+(MONTH+1)+"-"+DATE+"-"+HOUR+"-"+MINUTE+"-"+SECOND;
return time;}}
However, it always show the same time. What have I done wrong.
P.S. it is intended not to use command like gettime() directely. The string should remain there.
Upvotes: 1
Views: 825
Reputation: 86379
Your title says “applying GregorianCalendar
”, and that’s not what I am doing in this answer. While mustabelMo’s answer is correct, I wanted to show you how IMHO your task is best solved, and the first point is I recommend you don’t use the long outdated GregorianCalendar
class. Today we have so much better in the modern Java date and time API known as java.time
or JSR-310. For your need, you may use a LocalDateTime
:
import java.time.ZoneId;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class MyCalendar {
public static void main(String[] args) {
MyCalendar a = new MyCalendar();
System.out.println(a.getCurrentTime());
}
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("u-M-d-H-m-s");
private LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
private String getCurrentTime() {
return now.format(formatter);
}
}
The program prints something like
2017-11-15-11-59-1
Other points to note:
Prefer composition over inheritance. Rather than inheriting from whatever class you use for getting calendar data, it’s better style to encapsulate an object of that class inside your class.
Use a formatter for formatting your date-time into a string, it’s more convenient and easier to change if you want a different format. For example, many will prefer to have the minutes and seconds printed in two digits always, with a leading zero as necessary. To obtain this, just use two format pattern letters instead of one:
private static final DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("u-M-d-H-mm-ss");
Now the output goes like
2017-11-15-12-03-01
In the code I use ZoneId.systemDefault()
to pick up the JVM’s time zone setting. This is fragile, it may be changed behind your back without notice. If you can, prefer a specific time zone in the region/city format, for example:
private LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Hong_Kong"));
Despite the name getCurrentTime()
gives you the time the MyCalendar
object was created, not the time the method was called. If you prefer the latter, just create a new LocalDateTime
object within the method.
You may prefer to use ZonedDateTime
or OffsetDateTime
instead of LocalDateTime
since these include a time zone and an offset from UTC, respectively. It’s often considered bad practice to throw this information away even when you don’t see any immediate use for it.
Upvotes: 0
Reputation: 1219
By using YEAR, MONTH... and so on, Here you are accessing static fields inherited from the GregorianCalendar class. However, you have to access the fields of your Calendar using these variables in somthing like this :
public String getCurrenttime(){
String time= this.get(YEAR)+"-"+(this.get(MONTH)+1)+"-"+
this.get(DATE)+"-"+this.get(HOUR_OF_DAY)+"-"+this.get(MINUTE)+"-"+this.get(SECOND);
return time;
}
Upvotes: 0