If you think the Android project sthlmtraveling listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2009-2014 Johan Nilsson <http://markupartist.com>
*//fromwww.java2s.com
* 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 com.markupartist.sthlmtraveling.utils;
import android.content.res.Resources;
import com.markupartist.sthlmtraveling.R;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Date & Time utils
*/publicclass DateTimeUtil {
publicstaticfinallong SECOND_IN_MILLIS = 1000;
publicstaticfinallong MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
publicstaticfinallong HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
publicstaticfinallong DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
publicstaticfinallong WEEK_IN_MILLIS = DAY_IN_MILLIS * 7;
/**
* Constructs a Date from the provided date and time.
*
* @param dateString In the yy.MM.dd format
* @param timeString In the HH:mm format
* @return A Date or null if failed to process the provided strings.
*/publicstatic Date fromSlDateTime(final String dateString, final String timeString) {
String dateTime = String.format("%s %s", dateString, timeString);
Date date = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yy HH:mm");
try {
date = simpleDateFormat.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* Return given duration in a human-friendly format. For example, "4
* minutes" or "1 second". Returns only largest meaningful unit of time,
* from seconds up to hours.
* <p/>
* From android.text.format.DateUtils
*/publicstatic CharSequence formatDuration(final Resources res, finallong millis) {
if (millis >= HOUR_IN_MILLIS) {
finalint hours = (int) ((millis + 1800000) / HOUR_IN_MILLIS);
return res.getQuantityString(R.plurals.duration_hours, hours, hours);
} elseif (millis >= MINUTE_IN_MILLIS) {
finalint minutes = (int) ((millis + 30000) / MINUTE_IN_MILLIS);
return res.getQuantityString(R.plurals.duration_minutes, minutes, minutes);
} else {
finalint seconds = (int) ((millis + 500) / SECOND_IN_MILLIS);
return res.getQuantityString(R.plurals.duration_seconds, seconds, seconds);
}
}
}