0

我卡在某事上了。我想要的是获取系统日期和时间,将其存储在字符串中,然后在其他字符串中添加随机时间,然后使用 if else 将它们相互比较(例如

if(dateTime1 > dateTime2) {

}

然后我想显示 gmail 中显示的消息(如果邮件是今天发送的,gmail 会显示今天下午 5 点的时间)。我想在 text Boxes 中显示所有这些。我想要今天下午 4 点、昨天下午 4 点这样的消息,如果超过一周,则只需显示日期和时间。需要你的帮助希望你得到我想要的。请帮助我。这是我到目前为止所做的。还尝试了很多其他的东西。

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView ShowDate = (TextView) findViewById(R.id.ShowDate);
    TextView ShowToday = (TextView) findViewById(R.id.ShowToday);
    TextView ShowWeek  = (TextView) findViewById(R.id.ShowWeek );
    TextView ShowAgo  = (TextView) findViewById(R.id.ShowAgo ); 
    Calendar ci = Calendar.getInstance();

    String CiDateTime = "" + ci.get(Calendar.YEAR) + "-" +
        (ci.get(Calendar.MONTH) + 1) + "-" +
        ci.get(Calendar.DAY_OF_MONTH) + " " +
        ci.get(Calendar.HOUR) + ":" +
        ci.get(Calendar.MINUTE) +  ":" +
        ci.get(Calendar.SECOND);
4

3 回答 3

0

这将帮助您以字符串格式存储日期 - SimpleDateFormat

要在 gmail 之类的 textview 中显示时间,您必须检查当前时间和存储时间,您将获得从字符串到数据的简单日期格式。有关比较日期,请参阅此 - 比较日期

于 2014-04-08T11:12:59.360 回答
0

哦,我将给出 ua 示例,我的代码中的一个 make 我采用当前时间的差异,并且从 1970 年 1 月 1 日开始我计算时间差异,可能这将帮助你把你的时间而不是我的时间放在所有代码中是使用

 Calendar cal = Calendar.getInstance();
    Date currentLocalTime = cal.getTime();
    DateFormat date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");  
    date.setTimeZone(TimeZone.getTimeZone("GMT")); 
    String localTime = date.format(currentLocalTime); 

    long currenttime = Constant.retunlongdate(localTime);
    long fixtimejan = Constant.retunlongdate("01/01/1970 00:00:00 AM");

    long nTimeStamp = (currenttime - fixtimejan)/1000;
    System.out.println("and result is == " + nTimeStamp);

 public static long retunlongdate(String givenDateString)
        {

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); 

        long timeInMilliseconds=0;
        try {
            Date mDate =sdf.parse(givenDateString);
              timeInMilliseconds = mDate.getTime();
            System.out.println("Date in milli :: " + timeInMilliseconds);
            return timeInMilliseconds;
        } catch (ParseException e) {
                    e.printStackTrace();
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
        return timeInMilliseconds;
    }
于 2014-04-08T11:21:24.420 回答
-1

如果要获取系统的当前日期和时间。您可以使用此代码。

long millis = System.currentTimeMillis();
Date date = new Date(millis);

获取日期对象后将其转换为字符串 ex:date.toString();

于 2014-04-08T11:38:34.057 回答