Chronometer doesn't start from 00:00:00 - java

I have a problem with the chronometer. When I start it, it begins from 01:00:00.
I don't know why. I think that my code is correct.
Can you understand what the problem is?
This is my code:
Chronometer crono = new Chronometer(this);
crono.setBase(SystemClock.elapsedRealtime());
crono.start();
When I print the time I call this method:
long time = SystemClock.elapsedRealtime() - totalTime.getBase();
Date date = new Date(time);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
formatter.format(date);
Thanks very much!

You're trying to format a date difference as a date. I'd say maybe timezones come into play?
scala> new java.util.Date(0)
res2: java.util.Date = Thu Jan 01 01:00:00 GMT 1970
scala> new SimpleDateFormat("HH:mm:ss").format(new Date(0))
res5: String = 01:00:00
You might want something like [DurationFormatUtils.formatDuration()](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DurationFormatUtils.html#formatDuration(long, java.lang.String)).

Related

Why does java make us write more code?

Why does java make us write more code?
e.g:
long time = 1509694664442L;
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
c.getTime().setTime(time); // doesn't update the calendar's time!
System.out.println(c.getTime());
// but this does
Date d = c.getTime();
d.setTime(time);
c.setTime(d);
System.out.println(c.getTime());
Output:
Fri Jun 22 16:30:06 BRT 2018
Fri Jun 22 16:30:06 BRT 2018
Fri Nov 03 04:37:44 BRT 2017
Can someone explain why this happens since calendar.getTime() doesn't return a new instance of java.util.Date?
Calendar.getTime() returns a new Date instance, so trying to change the value on the returned object is futile, it has no effect on the Calendar instance.
Take a look at the source:
public final Date getTime() {
return new Date(getTimeInMillis());
}
This clearly shows that changing anything on the returned date doesn't change the calendar object itself.
You could make the change, then call calendar.setTime with the modified date to go around this.
Why does java make us write more code than the normal?
Because you are using the old and outdated date and time classes. java.time, the modern Java date and time API, allows not only terser but also clearer code:
long time = 1_509_694_664_442L;
Instant i = Instant.ofEpochMilli(time);
System.out.println(i);
Or, since you asked for brevity:
Instant.ofEpochMilli( 1_509_694_664_442L ).toString()
Output:
2017-11-03T07:37:44.442Z
For something more human readable (for some of us :-)
ZoneId zone = ZoneId.of("America/Recife");
Locale brazil = Locale.forLanguageTag("pt-BR");
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.FULL)
.withLocale(brazil);
String formatted = i.atZone(zone).format(formatter);
System.out.println(formatted);
sexta-feira, 3 de novembro de 2017 04:37:44 Horário Padrão de Brasília
Calendar.getTime()
…since calendar.getTime() doesn't return a new instance of java.util.Date?
It does exactly that. Here’s the implementation in JDK 10:
public final Date getTime() {
return new Date(getTimeInMillis());
}
Link
Oracle tutorial: Date Time explaining how to use java.time.

DateFormat.format returns 01:00:00 with HH:mm:ss format

I am using android.text.format.DateFormat to convert a long to hours, minutes and seconds. Here is the code I am testing with:
DateFormat.format("HH:mm:ss", 1000)
This returns:
01:00:01
Why is this? According to the SimpleDateFormat reference it should return 00:00:01 but it doesn't. I have tried all combinations and still no difference. I might have to revert to using the TimeUnit.MILLISECONDS.to... methods if DateFormat won't work.
The DateFormat observes your local time zone. If I understand your question, then you need something like,
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String str = df.format(1000);
System.out.println(str);
Output is (as expected)
00:00:01

SimpleDateFormat function parse(String s) gives wrong date

As an input I have Date object(for example, exDate=Fri Aug 01 00:00:00 EEST 2014) that must be formated. After the parsing of the date, I get wrong date.
SimpleDateFormat sdf = new SimpleDateFormat(
"dd-MMM-YYYY hh.mm.ss.SSSSSSSSS aa", Locale.ENGLISH);
String dateStart = sdf.format(exDate);
Date dateF = sdf.parse(dateStart);
dateStart will be equal to
01-Aug-2014 12.00.00.000000000 AM
and the resut, dateF will be equal to
Sun Dec 29 00:00:00 EET 2013
So, after the parsing of a string with date, the result is wrong.
Maybe, somebody know the source of the problem? Or another way to format date in another SimpleDateFormat?
The problem is the YYYY which means:
Y Week year;
The actual year, which is what you are looking for would be yyyy.
I really recommend that you go in the link above to see the full list.
You should also replace the milliseconds to .SSS as you can't get more precise than that.

How to convert CET time to local time on device?

I have three different times:
time on server - "Wed, 19 Feb 2014 11:44 CET"
time of start meeting on server - "12:00h"
time on device- "13:49"
I need to get time of start meeting on device ...this-> "14:00h" or time to meeting this-> "11m"
I'm trying to get it by using :
long ts = System.currentTimeMillis();
Date localTime = new Date(ts);
String gmt_time="12:00h";
SimpleDateFormat format = new SimpleDateFormat("HH:mm'h'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d_date = null;
d_date = format.parse(gmt_time);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date fromGmt = new Date(d_date.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
String new_date=format.format(fromGmt);
But result in new_date is "15:00h" (I need "14:00h")
You assumption of CET = GMT on Wed, 19 Feb 2014 seems to be incorrect - refer to here.
CET is an hour ahead of GMT
11:44 CET would mean 10:44 GMT
Hence, when you calculate an offset from GMT time and your local time, it adds an hour to it.
Change format.setTimeZone(TimeZone.getTimeZone("GMT")); to format.setTimeZone(TimeZone.getTimeZone("CET")); and it should work as expected.
Found quickly solution
String cet_time="12.11.14"+" "+"12:00h";
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yy' 'HH:mm'h'");
format.setTimeZone(TimeZone.getTimeZone("CET"));
Date d_date = format.parse(cet_time);
( new SimpleDateFormat("dd.MM.yy")).format(fromGmt);//new date String
( new SimpleDateFormat("HH:mm'h'")).format(fromGmt);//new time String

java.util.date/Calendar/SimpleDateFormat: Adding/Incrementing the months in java

I have tried the following; but the results are disappointing.
I want to increment the the months.
String dStartTime="2012-03-01";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-DD");
Date dateStartTime = dateFormatter.parse(dStartTime);
Calendar cal = Calendar.getInstance();
cal.setTime(dateStartTime);
cal.add(Calendar.MONTH, 1);
System.out.println(cal.getTime());
System.out.println(dateFormatter.format(cal.getTime()));
OUTPUT
Wed Feb 01 00:00:00 IST 2012 --- This is correct
2012-02-32 --- This is wrong. I want the Day should be one.
Please let me know what is the problem here?
Change new SimpleDateFormat("yyyy-MM-DD") to new SimpleDateFormat("yyyy-MM-dd").
DD is "Day in year" but you need dd "Day in month".
See http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html for Date and Time Patterns.

Categories