获取系统日期和时间将其存储在字符串Android中

时间:2014-04-08 11:10:36

标签: java android android-layout android-timepicker

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

if(dateTime1 > dateTime2) {

}

然后我想显示在gmail中显示的消息(如果今天发送邮件,则gmail显示今天下午5点的时间)。我想在文本框中显示所有内容。我希望像今天下午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);

3 个答案:

答案 0 :(得分:0)

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

要在文本视图中显示时间,例如gmail,您必须检查当前时间与存储时间,您将从字符串到数据形成简单的日期格式。 为比较日期,请参阅此 Compare date

答案 1 :(得分:0)

哦,我会给你一个例子,我的代码中的一个制作我采取时间的差异当前和从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;
    }

答案 2 :(得分:-1)

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

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

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

相关问题