如何在android中获取完整日期?

发布于 2024-12-05 09:29:01 字数 351 浏览 1 评论 0原文

我知道要借助日历实例在 android 中获取日期。

 Calendar c = Calendar.getInstance();

 System.out.println("====================Date is:"+ c.get(Calendar.DATE));

但这样我只得到了日期的号码。 。 。 在我的应用程序中,我必须根据日期格式进行一些计算。因此,如果月份发生变化,那么计算就会出错。 因此,出于这个原因,我想要给出月份、年份和当前日期的完整日期。 如果我想根据该日期进行一些计算,应该怎么做? 就像:如果日期少于两周,则应打印该消息。 。 。

请在这方面指导我。 谢谢。

I know about to get the date in android with the help of the calender instance.

 Calendar c = Calendar.getInstance();

 System.out.println("====================Date is:"+ c.get(Calendar.DATE));

But with that i got only the number of the Date. . .
In My Application i have to do Some Calculation based on the Date Formate. Thus if the months get changed then that calculation will be getting wrong.
So for that reason i want the full date that gives the Month, Year and the date of the current date.
And what should be done if i want to do Some Calculation based on that date ?
As like: if the date is less then two weeks then the message should be printed. . .

Please Guide me in this.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

罗罗贝儿 2024-12-12 09:29:01

看这里,

Date cal=Calendar.getInstance().getTime();
String date = SimpleDateFormat.getDateInstance().format(cal);

完整的日期格式请看 SimpleDateFormat

,如果你想的话对日期实例进行计算我认为您应该使用 Calendar.getTimeInMillis() 字段对这些毫秒进行计算。

编辑:这些是 SImpleDateFormat 类的格式。

String[] formats = new String[] {
  "yyyy-MM-dd",
  "yyyy-MM-dd HH:mm",
  "yyyy-MM-dd HH:mmZ",
  "yyyy-MM-dd HH:mm:ss.SSSZ",
  "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
 };
for (String format : formats) {
   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
}

编辑:两个日期差异(编辑日期:09/21/2011)

String startTime = "2011-09-19 15:00:23"; // this is your date to compare with current date 

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
            Date date1 = dateFormat.parse(startTime);

     // here I make the changes.... now Date d use a calendar's date 

           Date d = Calendar.getInstance().getTime(); // here you can use calendar beco'z date is now deprecated ..

           String systemTime =(String) DateFormat.format("yyyy-MM-dd HH:mm:ss", d.getTime());

           SimpleDateFormat df1;

           long diff = (d.getTime() - date1.getTime()) / (1000);

           int Totalmin =(int) diff / 60;
           int hours= Totalmin/60;
           int day= hours/24;
           int min = Totalmin % 60;
           int second =(int) diff % 60;


if(day < 14)
 {
  // your stuff here ... 
  Log.e("The day is within two weeks");
 }
 else
  {
   Log.e("The day is more then two weeks");
  }         

谢谢。

Look at here,

Date cal=Calendar.getInstance().getTime();
String date = SimpleDateFormat.getDateInstance().format(cal);

for full date format look SimpleDateFormat

and IF you want to do calculation on date instance I think you should use, Calendar.getTimeInMillis() field on these milliseconds make calculation.

EDIT: these are the formats by SImpleDateFormat class.

String[] formats = new String[] {
  "yyyy-MM-dd",
  "yyyy-MM-dd HH:mm",
  "yyyy-MM-dd HH:mmZ",
  "yyyy-MM-dd HH:mm:ss.SSSZ",
  "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
 };
for (String format : formats) {
   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
}

EDIT: two date difference (Edited on Date:09/21/2011)

String startTime = "2011-09-19 15:00:23"; // this is your date to compare with current date 

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
            Date date1 = dateFormat.parse(startTime);

     // here I make the changes.... now Date d use a calendar's date 

           Date d = Calendar.getInstance().getTime(); // here you can use calendar beco'z date is now deprecated ..

           String systemTime =(String) DateFormat.format("yyyy-MM-dd HH:mm:ss", d.getTime());

           SimpleDateFormat df1;

           long diff = (d.getTime() - date1.getTime()) / (1000);

           int Totalmin =(int) diff / 60;
           int hours= Totalmin/60;
           int day= hours/24;
           int min = Totalmin % 60;
           int second =(int) diff % 60;


if(day < 14)
 {
  // your stuff here ... 
  Log.e("The day is within two weeks");
 }
 else
  {
   Log.e("The day is more then two weeks");
  }         

Thanks.

还给你自由 2024-12-12 09:29:01

使用 SimpleDateFormat 类,

String date = SimpleDateFormat.getDateInstance().format(new Date());

Use SimpleDateFormat class,

String date = SimpleDateFormat.getDateInstance().format(new Date());
疯狂的代价 2024-12-12 09:29:01

您可以使用

//try different flags for the last parameter 
DateUtils.formatDateTime(context,System.currentTimeMillis(),DateUtils.FORMAT_SHOW_DATE);

所有选项检查 http://developer.android.com/参考/android/text/format/DateUtils.html

you can use

//try different flags for the last parameter 
DateUtils.formatDateTime(context,System.currentTimeMillis(),DateUtils.FORMAT_SHOW_DATE);

for all options check http://developer.android.com/reference/android/text/format/DateUtils.html

伏妖词 2024-12-12 09:29:01

试试这个,

int month = c.get(Calendar.MONTH);
  int year = c.get(Calendar.YEAR);
  int day = c.get(Calendar.DAY_OF_MONTH);
  System.out.println("Current date : " 
  + day + "/" + (month + 1) + "/" + year);
  }

try this,

int month = c.get(Calendar.MONTH);
  int year = c.get(Calendar.YEAR);
  int day = c.get(Calendar.DAY_OF_MONTH);
  System.out.println("Current date : " 
  + day + "/" + (month + 1) + "/" + year);
  }
念三年u 2024-12-12 09:29:01

我正在使用以下方法来获取日期和时间。您可以将此处的区域设置更改为阿拉伯语或您希望获取特定语言的日期。

public static String getDate(){
    String strDate;
    Locale locale = Locale.US;
    Date date = new Date();
    strDate = DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(date);

    return strDate;
}

public static String getTime(){
    String strTime;
    Locale locale = Locale.US;
    Date date = new Date();
    strTime = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).format(date);

    return strTime;
}

您可以获取该值并将其保存在字符串中,如下所示

String Date= getDate();
String Time = getTime();

I'm using following methods to get date and time. You can change the locale here to arabic or wot ever u wish to get date in specific language.

public static String getDate(){
    String strDate;
    Locale locale = Locale.US;
    Date date = new Date();
    strDate = DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(date);

    return strDate;
}

public static String getTime(){
    String strTime;
    Locale locale = Locale.US;
    Date date = new Date();
    strTime = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).format(date);

    return strTime;
}

you can get the value and save it on String as below

String Date= getDate();
String Time = getTime();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文