Skip to content

Instantly share code, notes, and snippets.

@alashow
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alashow/c38774df17c94791020d to your computer and use it in GitHub Desktop.
Save alashow/c38774df17c94791020d to your computer and use it in GitHub Desktop.
TimeAgo
/*
* Copyright 2014. Alashov Berkeli
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tm.veriloft.ertir.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static SimpleDateFormat hourMinuteFormat = new SimpleDateFormat("HH:mm");
/**
* Convert Java {@link java.util.Date} objects in just "a few minutes"
*
* @param date date object for convert
* @return converted readable date
*/
public static String getTimeAgo( Date date ) {
if (date == null) {
return null;
}
String timeAgo;
Date curDate = currentDate();
long time = date.getTime();
long now = curDate.getTime();
if (time > now || time <= 0) return null;
int dim = getTimeDistanceInMinutes(time);
if (dim == 0)
if (getTimeDistanceInSeconds(time) < 10) timeAgo = "şuwagtjyk";
else timeAgo = getTimeDistanceInSeconds(time) + " sekunt öň";
else if (dim == 1)
return "1 " + "minut öň";
else if (dim >= 2 && dim <= 44)
timeAgo = dim + " minut öň";
else if (dim >= 45 && dim <= 69)
timeAgo = "1 sagat öň";
else if (dim >= 70 && dim <= 1439)
timeAgo = (Math.round(dim / 60)) + " sagat öň" + ", " + hourMinuteFormat.format(date.getTime());
else if (dim >= 1440 && dim <= 2519)
timeAgo = "1 gün öň" + ", " + hourMinuteFormat.format(date.getTime());
else if (dim >= 2520 && dim <= 43199)
timeAgo = (Math.round(dim / 1440)) + " gün öň" + ", " + hourMinuteFormat.format(date.getTime());
else if (dim >= 43200 && dim <= 86399)
timeAgo = "1 aý öň, " + getHumanDateWithoutYear(time);
else if (dim >= 86400 && dim <= 525599)
timeAgo = (Math.round(dim / 43200)) + " aý öň, " + getHumanDateWithoutYear(time);
else
timeAgo = getHumanDate(time);
return timeAgo;
}
private static int getTimeDistanceInMinutes( long time ) {
long timeDistance = currentDate().getTime() - time;
return Math.round((Math.abs(timeDistance) / 1000) / 60);
}
private static int getTimeDistanceInSeconds( long time ) {
long timeDistance = currentDate().getTime() - time;
return Math.round((Math.abs(timeDistance) / 1000));
}
private static Date currentDate() {
Calendar calendar = Calendar.getInstance();
return calendar.getTime();
}
/**
* @param time timestamp in milliseconds
* @return "01 Ýan 2015ý 22:15" like date
*/
private static String getHumanDate( long time ) {
Calendar date = Calendar.getInstance();
date.setTimeInMillis(time);
return date.get(Calendar.DAY_OF_MONTH) + " " + getShortMonthName(date.get(Calendar.MONTH)) + " " + date.get(Calendar.YEAR) + "ý " + hourMinuteFormat.format(date.getTime());
}
/**
* @param time timestamp in milliseconds
* @return "01 Ýan 22:15" like date
*/
private static String getHumanDateWithoutYear( long time ) {
Calendar date = Calendar.getInstance();
date.setTimeInMillis(time);
return date.get(Calendar.DAY_OF_MONTH) + " " + getShortMonthName(date.get(Calendar.MONTH)) + " " + hourMinuteFormat.format(date.getTime());
}
/**
* Get Short Localised Month name by month index
*
* @param month index of month, in 0-11 range
* @return short month name
*/
private static String getShortMonthName( int month ) {
if (month < 0 || month > 11) return "null";
String[] months = {"Ýan", "Few", "Mart", "Apr", "Maý", "Iýun", "Iýul", "Awg", "Sen", "Okt", "Noý", "Dek"};
return months[month];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment