1

Every time I try to get the current time (I have a button for that, lets call it "botonGuardarEstado") I get the same hours and minutes. What I have noted is that the time I got is the time when I opened the app. What I mean is, if I opened the app at 7:10 a.m. and press the button at 7:12 a.m., I get 7:10 a.m. Here is my code:

DateFormat formatoFecha = new SimpleDateFormat("HH:mm dd/MM/yyyy");
String fecha = formatoFecha.format(Calendar.getInstance().getTime());

I am not getting weird values like different years or anything like that, and the format works well, the problem is that i get the same hours:minutes everytime i push the button. I alredy tried different ways of getting the date and time, things like Date(), or even getting only the hours and minutes using something like this

Calendar cal = Calendar.getInstance();
int mins = cal.get(Calendar.MINUTE);

but still got the same values.

I have the following class

private class InfoArchivo {
    String temperatura, humedad, gas, humo, iluminacion, riego, ventilacion, fecha;

    public InfoArchivo(String temperatura, String humedad, String gas, String humo, String iluminacion, String riego, String ventilacion, String fecha){
        this.temperatura = temperatura;
        this.humedad = humedad;
        this.gas = gas;
        this.humo = humo;
        this.iluminacion = iluminacion;
        this.riego = riego;
        this.fecha = fecha;

        if(!ventilacion.equals("0"))
            this.ventilacion = "1";
        else
            this.ventilacion = "0";
    }

I have an array of instances of that class. What i am trying to do is write a csv file using the array. Every other data (temperatura, humedad, etc) is correct. The only thing causing trouble is the date (fecha). The creation of the csv file is done until i press another button. When i press the botonGuardarEstado button i get the date, make an instance of the class InfoArchivo and add it to the array

EDIT: Also tried with this but still have the same issue:

Instant instant = Instant.now();
        ZoneId zoneId = ZoneId.of("America/Guatemala");
        ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formato = DateTimeFormatter.ofPattern("HH:mm dd/MM/yyyy");
        String fecha = zdt.format(formato);
Luis Garcia
  • 89
  • 1
  • 6
  • Possible duplicate of [Getting Current date and time](http://stackoverflow.com/questions/32585237/getting-current-date-and-time) – Ken Y-N Mar 23 '16 at 01:20
  • Possible duplicate of [Get current time and date on Android](http://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android) – Basil Bourque Mar 23 '16 at 01:25

1 Answers1

0

Not a date-time problem

Your code as shown is correct.

So your problem must be happening elsewhere in your app. Perhaps you are not correctly feeding the new String (fecha) to the user interface. Or perhaps you need to do something to refresh the display of that new String’s value. We cannot help you further as you did not provide that other code.

java.time

By the way, you are using outmoded classes. The old date-time classes that have proven to be so confusing and troublesome have been supplanted by the java.time framework. See the Oracle Tutorial.

Instant

An Instant is a moment on the timeline in UTC with resolution up to nanoseconds.

 Instant instant = Instant.now(); // Current moment in UTC.

Time Zone

Apply a time zone (ZoneId) to get a ZonedDateTime. If you omit the time zone your JVM’s current default time zone is implicitly applied. Better to specify explicitly the desired/expected time zone.

ZoneId zoneId = ZoneId.of( "America/Montreal" ); // Or "Asia/Kolkata", "Europe/Paris", and so on.
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

Generating Strings

You can easily generate a String as a textual representation of the date-time value. You can go with a standard format, your own custom format, or an automatically localized format.

ISO 8601

You can call the toString methods to get text formatted using the common and sensible ISO 8601 standard.

String output = instant.toString();

2016-03-19T05:54:01.613Z

Custom format

Or specify your own particular formatting pattern with the DateTimeFormatter class.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/MM/yyyy hh:mm a" );

Specify a Locale for a human language (English, French, etc.) to use in translating the name of day/month and also in defining cultural norms such as the order of year and month and date. Note that Locale has nothing to do with time zone.

formatter = formatter.withLocale( Locale.US ); // Or Locale.CANADA_FRENCH or such.
String output = zdt.format( formatter );

Localizing

Better yet, let java.time do the work of localizing automatically.

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM );
String output = zdt.format( formatter.withLocale( Locale.US ) );  // Or Locale.CANADA_FRENCH and so on.
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks, i update my question trying to be more specific. I wil try using Instant. Can i use something like SimpleDateFormat with ZonedDateTime? – Luis Garcia Mar 23 '16 at 02:50
  • @LuisGarcia No, do not mix the old and new date-time classes (except to convert data). With `ZonedDateTime` use the [`DateTimeFormatter`](http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) class to generate strings. I just expanded my Answer to explain more. – Basil Bourque Mar 23 '16 at 03:01
  • I tried whit Instant, but still gives the same hour and minutes in wich i opened the app. I openned the app at 22:20, and waited 1 and 2 minutes to press the button (pressing it at 22:21 and 22:22) but still i got 22:20 in both cases. I will post the code in the question – Luis Garcia Mar 23 '16 at 03:29
  • Thanks for your help, i already found the problem, it was a REALLY stupid error form my part. I was getting the date outside the onClick method.... – Luis Garcia Mar 23 '16 at 04:12
  • @LuisGarcia Using [logging](http://slf4j.org/) or calling `System.out.println` should show that kind of error quickly. – Basil Bourque Mar 23 '16 at 05:01