0

I am trying to get a QR code of current date for an attendance system. i am setting the value to a TextView. I am doing something like this..

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.mm.yyyy");
Date date = new Date();
String data = simpleDateFormat.format(date);
textView.setText(data);

I am supposed to get something like 25.09.2021

Instead I am getting java.text.SimpleDateFormat@44aa2260

what am i doing wrong?

  • 3
    Nope, works fine for me, must be something else you're doing. Oh, and `mm` is "minute of hour". Mind you now days, you should be doing something more like `DateTimeFormatter.ofPattern("dd.MM.yyyy").format(LocalDate.now())` – MadProgrammer Sep 25 '21 at 06:38
  • 2
    Your actual code is `textView.setText(simpleDateFormat);` - how do I know? Because you get `java.text.SimpleDateFormat@44aa2260` by calling `simpleDateFormat.toString()` – Elliott Frisch Sep 25 '21 at 06:39
  • Thanks @MadProgrammer for suggestion. i will keep that in mind. –  Sep 25 '21 at 07:00
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 25 '21 at 07:02

2 Answers2

1

tl;dr

    LocalDate
    .now(
        ZoneId.of( "Africa/Tunis" )
    )
    .format(
        DateTimeFormatter.ofPattern( "dd.MM.uuuu" )
    )

Automatically localize:

LocalDate.now().format( DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ) )

Or:

LocalDate
.now( ZoneId.of( "Asia/Kolkata" ) )
.format( 
    DateTimeFormatter
    .ofLocalizedDate( FormatStyle.SHORT )
    .withLocale( 
        new Locale( "hi" , "IN" ) 
    ) 
)

SimpleDateFormat#toString

As commented by Frisch, your actual code differs from what you showed in your Question. We know that because a value like java.text.SimpleDateFormat@44aa2260 is what is produced by SimpleDateFormat#toString, having inherited that method implementation from Object#toString.

So you must be calling something like textView.setText( simpleDateFormat );.

Another problem: Your formatting pattern is incorrect. The codes are case-sensitive. So mm should be MM.

java.time

You are using terrible date-time classes that are now legacy, supplanted years ago by the modern java.time classes defined in JSR 310.

I recommend being explicit about the time zone by which to determine today’s date rather than relying implicitly on the JVM’s current default time zone.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;  // Or `ZoneId.systemDefault()`. 
LocalDate today = LocalDate.now( z ) ;

Specify your formatting pattern.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uuuu" ) ;
String output = today.format( f ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0
import java.text.SimpleDateFormat;
import java.util.Date;
public class CurrentDateTimeExample2 {
public static void main(String[] args) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
    Date date = new Date();
    System.out.println(formatter.format(date));
   }
}

Took reference from here

  • That reference is long outdated. Please don’t use it. The example dates are from between 2001 and 2012 (with one in 1818), so probably it was written in 2001 and later updated, but not after the advent of java.time, the modern Java date and time API, in 2014 (seven and a half years ago and counting). It would need a complete rewrite from scratch. Unfortunately many such outdated tutorials still lie around on the Internet. Instead you may for example use [the official Oracle tutorial](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Sep 25 '21 at 08:39