Java tutorial
/* The MIT License (MIT) Copyright (c) 2015 QAXML Pty Ltd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.qatickets.domain.common; import java.util.Date; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import com.qatickets.domain.UserProfile; public class DateHelper { 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; static { DateTimeZone.setDefault(DateTimeZone.UTC); } /* * Copyright 2012 Google Inc. */ public static String getTimeAgo(long time) { if (time < 1000000000000L) { // if timestamp given in seconds, convert to millis time *= 1000; } long now = System.currentTimeMillis(); if (time > now || time <= 0) { return "just now"; } // TODO: localize final long diff = now - time; if (diff < MINUTE_MILLIS) { return "just now"; } else if (diff < 2 * MINUTE_MILLIS) { return "a minute ago"; } else if (diff < 50 * MINUTE_MILLIS) { return diff / MINUTE_MILLIS + " minutes ago"; } else if (diff < 90 * MINUTE_MILLIS) { return "an hour ago"; } else if (diff < 24 * HOUR_MILLIS) { return diff / HOUR_MILLIS + " hours ago"; } else if (diff < 48 * HOUR_MILLIS) { return "yesterday"; } else { return diff / DAY_MILLIS + " days ago"; } } public static String PATTERN_1 = "dd MMM yyyy hh:mm a"; public static String currentDate(UserProfile user, String pattern) { DateTime dt = new DateTime(user.getDateTimeZone()); DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern); return dtf.print(dt); } public static DateTime toUserTimeZone(UserProfile user, Date dateUTC) { DateTime dt = new DateTime(dateUTC, DateTimeZone.UTC); return dt.toDateTime(user.getDateTimeZone()); } public static String format(UserProfile user, Date dateUTC, String pattern) { if (dateUTC == null) { return null; } DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern); return dtf.print(toUserTimeZone(user, dateUTC)); } public static Date nowUTC() { return new DateTime(DateTimeZone.UTC).toDate(); } }