2010-10-11 12 views
6

मैं करने के लिए:जावा (Android) SQLite तारीख कन्वर्ट "एक्स दिन पहले"

String date = "2010-10-9 12:00:00"; 

मुझे लगता है कि स्ट्रिंग पार्स चाहते हैं, तो आज की तारीख से घटाना है कि दिनांक/समय/समय ताकि मैं उत्पादन कर सकते हैं "2 दिन पहले" की तरह एक स्ट्रिंग।

उत्तर

1

इसे आज तक कनवर्ट करें। वर्तमान दिनांक समय प्राप्त करें। अंतर कैल्क।

+0

हां, यही वास्तव में मुझे क्या करना चाहते हैं। अब अगर कोई मुझे यह दिखाने के लिए पर्याप्त दयालु हो सकता है कि इसे कैसे किया जाए। – Paul

2

ऐसा करने का एक बेहतर तरीका हो सकता है, लेकिन यह काम करता है।

String dateString = "2010-10-9 12:00:00"; 
String daysAgo = null; 
// How many milliseconds in 1 day 
final long DAY_IN_MILLIS = 86400000; 
// The current timestamp in milliseconds 
long now = System.currentTimeMillis(); 
// The format of your date string assuming the 1 am would read 01:00, not 1:00 
// and Jan 1, 2010 would read 2010-1-1, not 2010-01-01 
final DateFormat formatter = new SimpleDateFormat("yyyy-M-d hh:mm:ss"); 
// The calendar instance which adds a locale to the date 
final Calendar cal = Calendar.getInstance(); 
try { 
    // Parse the date string to return a Date object 
    Date date = formatter.parse(dateString); 
    // Set the calendar with our date object 
    cal.setTime(date); 
    // Get the millis timestamp of your date string 
    long then = cal.getTimeInMillis(); 
    // Calculate the difference 
    long difference = now - then; 
    int ago = 0; 
    // If the difference is greater than one day 
    if (difference >= DAY_IN_MILLIS) { 
     // Find the product 
     ago = (int) (difference/DAY_IN_MILLIS); 
     // Format your new string 
      // You may want to check if(ago>1) here 
     daysAgo = String.format("%d day(s) ago", ago); 
    } 
    // Write the result to Logcat 
    Log.d(TAG, daysAgo); 

} catch (ParseException e) { 
    Log.e(TAG, e.getLocalizedMessage()); 
} 
18

यह एक सहायक वर्ग है जिसका उपयोग मैं एंड्रॉइड के मानक दिनांक उपयोग को विस्तारित कर रहा हूं। इसमें एक उन्नत तर्क है, कि आज के टाइमस्टैम्प के लिए, यह सेकंड या मिनट या घंटे प्रदर्शित करेगा, जबकि अन्य टाइमस्टैम्प के लिए यह तारीख प्रदर्शित करेगा।

आप getTimeDiffString विधि में अपनी आवश्यकताओं के लिए तर्क समायोजित कर सकते हैं। पैरामीटर के रूप में, आप Date date = formatter.parse(dateString); के टाइमस्टैम्प को पार्स करेंगे जो आप उपरोक्त कोड में ला रहे हैं।

कोड तर्क 'टाइमस्टैम्प डिस्प्ले' का अनुपालन करता है जैसा कि आप इसे फेसबुक या ट्विटर से जानते हैं।

public class DateTimeUtils extends DateUtils { 

    private static String mTimestampLabelYesterday; 
    private static String mTimestampLabelToday; 
    private static String mTimestampLabelJustNow; 
    private static String mTimestampLabelMinutesAgo; 
    private static String mTimestampLabelHoursAgo; 
    private static String mTimestampLabelHourAgo; 

    /** 
    * Singleton contructor, needed to get access to the application context & strings for i18n 
    * @param context Context 
    * @return DateTimeUtils singleton instanec 
    * @throws Exception 
    */ 
    public static DateTimeUtils getInstance(Context context) { 
     mCtx = context; 
     if (instance == null) { 
      instance = new DateTimeUtils(); 
      mTimestampLabelYesterday = context.getResources().getString(R.string.WidgetProvider_timestamp_yesterday); 
      mTimestampLabelToday = context.getResources().getString(R.string.WidgetProvider_timestamp_today); 
      mTimestampLabelJustNow = context.getResources().getString(R.string.WidgetProvider_timestamp_just_now); 
      mTimestampLabelMinutesAgo = context.getResources().getString(R.string.WidgetProvider_timestamp_minutes_ago); 
      mTimestampLabelHoursAgo = context.getResources().getString(R.string.WidgetProvider_timestamp_hours_ago); 
      mTimestampLabelHourAgo = context.getResources().getString(R.string.WidgetProvider_timestamp_hour_ago); 
     } 
     return instance; 
    } 

    /** 
    * Checks if the given date is yesterday. 
    * 
    * @param date - Date to check. 
    * @return TRUE if the date is yesterday, FALSE otherwise. 
    */ 
    public static boolean isYesterday(long date) { 

     final Calendar currentDate = Calendar.getInstance(); 
     currentDate.setTimeInMillis(date); 

     final Calendar yesterdayDate = Calendar.getInstance(); 
     yesterdayDate.add(Calendar.DATE, -1); 

     return yesterdayDate.get(Calendar.YEAR) == currentDate.get(Calendar.YEAR) && yesterdayDate.get(Calendar.DAY_OF_YEAR) == currentDate.get(Calendar.DAY_OF_YEAR); 
    } 

    public static String[] weekdays = new DateFormatSymbols().getWeekdays(); // get day names 
    public static final long millisInADay = 1000 * 60 * 60 * 24; 


    ... 

    /** 
    * Displays a user-friendly date difference string 
    * @param timedate Timestamp to format as date difference from now 
    * @return Friendly-formatted date diff string 
    */ 
    public String getTimeDiffString(long timedate) { 
     Calendar startDateTime = Calendar.getInstance(); 
     Calendar endDateTime = Calendar.getInstance(); 
     endDateTime.setTimeInMillis(timedate); 
     long milliseconds1 = startDateTime.getTimeInMillis(); 
     long milliseconds2 = endDateTime.getTimeInMillis(); 
     long diff = milliseconds1 - milliseconds2; 

     long hours = diff/(60 * 60 * 1000); 
     long minutes = diff/(60 * 1000); 
     minutes = minutes - 60 * hours; 
     long seconds = diff/(1000); 

     boolean isToday = DateTimeUtils.isToday(timedate); 
     boolean isYesterday = DateTimeUtils.isYesterday(timedate); 

     if (hours > 0 && hours < 12) { 
      return hours==1? String.format(mTimestampLabelHourAgo,hours) : String.format(mTimestampLabelHoursAgo,hours); 
     } else if (hours <= 0) { 
      if (minutes > 0) 
       return String.format(mTimestampLabelMinutesAgo,minutes); 
      else { 
       return mTimestampLabelJustNow; 
      } 
     } else if (isToday) { 
      return mTimestampLabelToday; 
     } else if (isYesterday) { 
      return mTimestampLabelYesterday; 
     } else if (startDateTime.getTimeInMillis() - timedate < millisInADay * 6) { 
      return weekdays[endDateTime.get(Calendar.DAY_OF_WEEK)]; 
     } else { 
      return formatDateTime(mCtx, timedate, DateUtils.FORMAT_NUMERIC_DATE); 
     } 
    } 

} 

जबकि strings.xml रखती है:

<string name="WidgetProvider_timestamp_today">Today</string> 
<string name="WidgetProvider_timestamp_yesterday">Yesterday</string> 
<string name="WidgetProvider_timestamp_hour_ago">%s hour ago</string> 
<string name="WidgetProvider_timestamp_hours_ago">%s hours ago</string> 
<string name="WidgetProvider_timestamp_minutes_ago">%s minutes ago</string> 
<string name="WidgetProvider_timestamp_just_now">Just now</string> 
+0

आपको आयात java.text.DateFormatSymbols आयात करना होगा; आयात java.util. कैलेंडर; आयात करें android.content.Context; आयात करें android.text.format.DateUtils; और अपनी कक्षा में दो चर घोषित करें: निजी स्थैतिक संदर्भ एमसीटीएक्स; निजी स्थैतिक तिथि समय उदाहरण के रूप में; – happyhardik

4

इस प्रयास करें:

long currentTimeMillis = System.currentTimeMillis(); 
    CharSequence string = DateUtils.getRelativeTimeSpanString(currentTimeMillis, currentTimeMillis + DateUtils.MINUTE_IN_MILLIS * 5, 0, DateUtils.FORMAT_ABBREV_ALL); 
संबंधित मुद्दे