Home > Software design >  How to return java.util.Date in this format dd/MM/yyyy?
How to return java.util.Date in this format dd/MM/yyyy?

Time:11-11

I'm using this code to show time in specific mode. But after three weeks I want to display time in normal mode, using simple time format. Below it's the code I tried so far:

}
static class TimeAgo {
        private static final int SECOND_MILLIS = 1000;
        private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
        private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
        private static final int DAY_MILLIS = 24 * HOUR_MILLIS;
private static final int WEEK_MILLIS = 7 * DAY_MILLIS ;


        public static String getTimeAgo(long time) {
            if (time < 1000000000000L) {
                time *= 1000;
            }

            long now = System.currentTimeMillis();
            if (time > now || time <= 0) {
                return null;
            }


            final long diff = now - time;
            if (diff < MINUTE_MILLIS) {
                return "Justo ahora";
            } else if (diff < 2 * MINUTE_MILLIS) {
                return "Hace 1 minuto";
            } else if (diff < 50 * MINUTE_MILLIS) {
                return "Hace "   diff / MINUTE_MILLIS   " minutos";
            } else if (diff < 90 * MINUTE_MILLIS) {
                return "Hace una hora";
            } else if (diff < 24 * HOUR_MILLIS) {
                return "Hace "   diff / HOUR_MILLIS   " horas";
            } else if (diff < 48 * HOUR_MILLIS) {
                return "Ayer";
} else if (diff < 72 * HOUR_MILLIS) {
                return "Hace un día";
           } else if (diff < 7 * DAY_MILLIS) {
                return "Hace "   diff / DAY_MILLIS   " días";
            } else if (diff < 2 * WEEK_MILLIS) {
                return "Hace una semana";
            } else if (diff < WEEK_MILLIS * 3) {
                return "Hace "   diff / WEEK_MILLIS   " semanas";
} else {
                java.util.Date date = new java.util.Date((long) time); return date.toString();
            }
        }
    }
{

But it doesn't return the time format needed. Any help will be appreciated.

CodePudding user response:

I really suggest you, to use java.time API, because Date class is mostly deprecated

So now what you can do is use java.time.format.DateTimeFormatter

Here is the basic example and also I am gonna assume you are writing the following code which I am using for example inside the class, Main method. So you can import the libraries by yourself

LocalDate date = 
your_date_.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

DateTimeFormatter dtformat = DateTimeFormatter.offPattern("dd/MM/yyyy");
YourDate = dtformat.format(date);
System.out.println(YourDate);

CodePudding user response:

Use this to format date:

  SimpleDateFormat timeFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.US);

  String formattedDate;

  Calendar calendar = Calendar.getInstance(); 
  calendar.setTimeInMillis(timeInMillis); // timeInMillis should be in milliseconds
  Date calendarTime = calendar.getTime(); 

  formattedDate = timeFormat.format(calendarTime);

CodePudding user response:

if you want to make date formate like 21-10-2021 you can use the this method:

private String getReadableDateTime(Date date) {
  return new SimpleDateFormat("dd-MM-yyyy" , Locale.getDefault()).format(date);
}

then when ever you want to get current date call the method like that

getReadableDateTime(new Date());

and it would give you the formate you want. addition if you want to get time with PM or AM use this "dd-MM-yyyy - hh:mm a"

CodePudding user response:

Already solved the issue. I just needed to change this:

java.util.Date date = new java.util.Date((long) time); return date.toString();

And insert this:

java.text.SimpleDateFormat f = new java.text.SimpleDateFormat(
                "dd/MM/yyyy");
        return f.format(time);

Thanks everyone (even the downvotes)

  • Related