0

Here's my code:

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

public static void main(String[] args) {
    Calendar cal = new GregorianCalendar();
    Date date = new Date(System.currentTimeMillis());
    cal.setTime( date );
    if (Calendar.MINUTE < 40) {
        if (Calendar.HOUR_OF_DAY == 0) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
            cal.set(Calendar.HOUR_OF_DAY, 23);
        }
        else
        cal.add(Calendar.HOUR_OF_DAY, -1);
    }
    int z = cal.get(Calendar.DAY_OF_MONTH);
    int w = cal.get(Calendar.HOUR_OF_DAY);
    SimpleDateFormat sdf_nowbasedate = new SimpleDateFormat("yyyyMMdd");
    SimpleDateFormat sdf_nowbasetime = new SimpleDateFormat("HH");

    String baseDate_now = sdf_nowbasedate.format(z);
    String baseTime_now = sdf_nowbasetime.format(w)+ "00";

System.out.println(baseDate_now + " " + baseTime_now);

Result that I expect is "20180606 1200"(Because now time writing this article is 2018 June 6th, 1:12 PM (KST - I live in Korea)

But the result is "19700101 0900"

Why does this error happen?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
San Koh
  • 3
  • 3
  • Have you debugged the if else condition? – Jaymin Jun 06 '18 at 04:26
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. `Date` and `Calendar` are outdated too. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). BTW, it’s always dangerous to assume there’s nothing wrong with your code. – Ole V.V. Jun 06 '18 at 07:14

3 Answers3

1
int z = cal.get(Calendar.DAY_OF_MONTH);
int w = cal.get(Calendar.HOUR_OF_DAY);
SimpleDateFormat sdf_nowbasedate = new SimpleDateFormat("yyyyMMdd");
String baseDate_now = sdf_nowbasedate.format(z);    

First off, this code can't even compile. There is no format function that takes an integer as a parameter. It takes a date. But assuming you're actually somehow making a date out of that number- that would make your date basically a few dozen ms off the epoch time, which is Jan 1, 1970.

Instead, pass the date in to format directly.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • 1
    FWIW, it can compile and produces this result since there's `format(Object)` in `java.text.DateFormat` / `java.text.Format` superclasses that takes any `Number` as millis-since-epoch value. – laalto Jun 06 '18 at 05:24
0

you must pass a Date to sdf_nowbasedate.format(Date) instead int ("z" or "w") as you are using Calendar you must get the Date

String baseDate_now = sdf_nowbasedate.format(cal.getTime());
String baseTime_now = sdf_nowbasetime.format(cal.getTime())+ "00";
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

java.time

The other answers have correctly explained why your code gave you a date of January 1, 1970. I should like to contribute the modern correct version of the code. I think you’re after something like this:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMdd HHmm");
    ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
    if (zdt.getMinute() < 40) {
        zdt = zdt.minusHours(1);
    }
    ZonedDateTime wholeHours = zdt.truncatedTo(ChronoUnit.HOURS);
    String baseNow = wholeHours.format(formatter);
    System.out.println(baseNow);

Running this just now (00:52 in South Korea) it printed:

20180607 0000

I am using java.time, the modern Java date and time API, because Calendar, GregorianCalendar, Date and SimpleDateFormat are all long outdated and poorly designed and java.time is so much nicer to work with.

There seems to be another bug in your code. Calendar.MINUTE and Calendar.HOUR_OF_DAY are constants with values 12 and 11, so Calendar.MINUTE < 40 will always be true, and Calendar.HOUR_OF_DAY == 0 will never be. Instead you probably intended cal.get(Calendar.MINUTE) and similarly for hour of day. Lesson to learn: with the old classes it is very easy to make errors, also some that go unnoticed and others that are hard to pinpoint. I do recommend you avoid those classes.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161