Search in sources :

Example 91 with SimpleDateFormat

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;
}
Also used : ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 92 with SimpleDateFormat

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();
}
Also used : DBManager(com.kiminonawa.mydiary.db.DBManager) Cursor(android.database.Cursor) SimpleDateFormat(java.text.SimpleDateFormat)

Example 93 with SimpleDateFormat

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());
        }
    }
}
Also used : ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 94 with SimpleDateFormat

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");
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 95 with SimpleDateFormat

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);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

SimpleDateFormat (java.text.SimpleDateFormat)2847 Date (java.util.Date)1590 ParseException (java.text.ParseException)463 DateFormat (java.text.DateFormat)425 Calendar (java.util.Calendar)307 Test (org.junit.Test)305 ArrayList (java.util.ArrayList)232 File (java.io.File)230 IOException (java.io.IOException)185 GregorianCalendar (java.util.GregorianCalendar)139 HashMap (java.util.HashMap)121 Locale (java.util.Locale)70 DateField (edu.uci.ics.textdb.api.field.DateField)64 DoubleField (edu.uci.ics.textdb.api.field.DoubleField)64 IField (edu.uci.ics.textdb.api.field.IField)64 IntegerField (edu.uci.ics.textdb.api.field.IntegerField)64 StringField (edu.uci.ics.textdb.api.field.StringField)63 TextField (edu.uci.ics.textdb.api.field.TextField)63 Map (java.util.Map)63 Tuple (edu.uci.ics.textdb.api.tuple.Tuple)61