use of java.text.SimpleDateFormat in project android-app by eoecn.
the class DateUtil method getDiff.
/**
* 获取两个时间串时间的差值,单位为秒
*
* @param startTime
* 开始时间 yyyy-MM-dd HH:mm:ss
* @param endTime
* 结束时间 yyyy-MM-dd HH:mm:ss
* @return 两个时间的差值(秒)
*/
public static long getDiff(String startTime, String endTime) {
long diff = 0;
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date startDate = ft.parse(startTime);
Date endDate = ft.parse(endTime);
diff = startDate.getTime() - endDate.getTime();
diff = diff / 1000;
} catch (ParseException e) {
e.printStackTrace();
}
return diff;
}
use of java.text.SimpleDateFormat in project MyDiary by erttyy8821.
the class DiaryViewerDialogFragment method initData.
private void initData() {
DBManager dbManager = new DBManager(getActivity());
dbManager.opeDB();
Cursor diaryInfoCursor = dbManager.selectDiaryInfoByDiaryId(diaryId);
//load Time
calendar = Calendar.getInstance();
calendar.setTimeInMillis(diaryInfoCursor.getLong(1));
timeTools = TimeTools.getInstance(getActivity().getApplicationContext());
sdf = new SimpleDateFormat("HH:mm");
setDiaryTime();
if (isEditMode) {
//Allow to edit diary
LL_diary_time_information.setOnClickListener(this);
EDT_diary_title.setText(diaryInfoCursor.getString(2));
} else {
String diaryTitleStr = diaryInfoCursor.getString(2);
if (diaryTitleStr == null || diaryTitleStr.equals("")) {
diaryTitleStr = getString(R.string.diary_no_title);
}
TV_diary_title_content.setText(diaryTitleStr);
}
//load location
String locationName = diaryInfoCursor.getString(7);
if (locationName != null && !"".equals(locationName)) {
haveLocation = true;
IV_diary_location_name_icon.setVisibility(View.VISIBLE);
TV_diary_location.setText(locationName);
} else {
haveLocation = false;
IV_diary_location_name_icon.setVisibility(View.VISIBLE);
}
initLocationIcon();
setIcon(diaryInfoCursor.getInt(3), diaryInfoCursor.getInt(4));
diaryInfoCursor.close();
//Get diary detail
loadDiaryItemContent(dbManager);
dbManager.closeDB();
}
use of java.text.SimpleDateFormat in project opentsdb by OpenTSDB.
the class DateTime method parseDateTimeString.
/**
* Attempts to parse a timestamp from a given string
* Formats accepted are:
* <ul>
* <li>Relative: {@code 5m-ago}, {@code 1h-ago}, etc. See
* {@link #parseDuration}</li>
* <li>Absolute human readable dates:
* <ul><li>"yyyy/MM/dd-HH:mm:ss"</li>
* <li>"yyyy/MM/dd HH:mm:ss"</li>
* <li>"yyyy/MM/dd-HH:mm"</li>
* <li>"yyyy/MM/dd HH:mm"</li>
* <li>"yyyy/MM/dd"</li></ul></li>
* <li>Unix Timestamp in seconds or milliseconds:
* <ul><li>1355961600</li>
* <li>1355961600000</li>
* <li>1355961600.000</li></ul></li>
* </ul>
* @param datetime The string to parse a value for
* @return A Unix epoch timestamp in milliseconds
* @throws NullPointerException if the timestamp is null
* @throws IllegalArgumentException if the request was malformed
*/
public static final long parseDateTimeString(final String datetime, final String tz) {
if (datetime == null || datetime.isEmpty())
return -1;
if (datetime.matches("^[0-9]+ms$")) {
return Tags.parseLong(datetime.replaceFirst("^([0-9]+)(ms)$", "$1"));
}
if (datetime.toLowerCase().equals("now")) {
return System.currentTimeMillis();
}
if (datetime.toLowerCase().endsWith("-ago")) {
long interval = DateTime.parseDuration(datetime.substring(0, datetime.length() - 4));
return System.currentTimeMillis() - interval;
}
if (datetime.contains("/") || datetime.contains(":")) {
try {
SimpleDateFormat fmt = null;
switch(datetime.length()) {
// break;
case 10:
fmt = new SimpleDateFormat("yyyy/MM/dd");
break;
case 16:
if (datetime.contains("-"))
fmt = new SimpleDateFormat("yyyy/MM/dd-HH:mm");
else
fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm");
break;
case 19:
if (datetime.contains("-"))
fmt = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");
else
fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
break;
default:
// todo - deal with internationalization, other time formats
throw new IllegalArgumentException("Invalid absolute date: " + datetime);
}
if (tz != null && !tz.isEmpty())
setTimeZone(fmt, tz);
return fmt.parse(datetime).getTime();
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid date: " + datetime + ". " + e.getMessage());
}
} else {
try {
long time;
final boolean contains_dot = datetime.contains(".");
// [0-9]{10} ten digits
// \\. a dot
// [0-9]{1,3} one to three digits
final boolean valid_dotted_ms = datetime.matches("^[0-9]{10}\\.[0-9]{1,3}$");
if (contains_dot) {
if (!valid_dotted_ms) {
throw new IllegalArgumentException("Invalid time: " + datetime + ". Millisecond timestamps must be in the format " + "<seconds>.<ms> where the milliseconds are limited to 3 digits");
}
time = Tags.parseLong(datetime.replace(".", ""));
} else {
time = Tags.parseLong(datetime);
}
if (time < 0) {
throw new IllegalArgumentException("Invalid time: " + datetime + ". Negative timestamps are not supported.");
}
// in seconds or milliseconds. This will work until November 2286
if (datetime.length() <= 10) {
time *= 1000;
}
return time;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid time: " + datetime + ". " + e.getMessage());
}
}
}
use of java.text.SimpleDateFormat in project opentsdb by OpenTSDB.
the class TestDateTime method setTimeZoneBadTZ.
@Test(expected = IllegalArgumentException.class)
public void setTimeZoneBadTZ() {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
DateTime.setTimeZone(fmt, "NotHere");
}
use of java.text.SimpleDateFormat in project android_frameworks_base by ParanoidAndroid.
the class AndroidKeyPairGeneratorTest method assertDateEquals.
private static void assertDateEquals(String message, Date date1, Date date2) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
String result1 = formatter.format(date1);
String result2 = formatter.format(date2);
assertEquals(message, result1, result2);
}
Aggregations