java 如何从 Android 中的日历对象获取日期。?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17760739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:53:49  来源:igfitidea点击:

How to get Date from Calender object in Android.?

javaandroid

提问by Dnyanesh M

I have Calender object as follows--

我有日历对象如下——

final Calendar calendar = Calendar.getInstance();

and I want to get/extract date from above Calendar object in the following format--

我想以下列格式从上面的日历对象中获取/提取日期--

"Sat Jul 20 14:53:36 GMT+02:00 2013"

Can anybody please tell me How can I do this..!!

谁能告诉我我该怎么做..!!

回答by amatellanes

Try this:

试试这个:

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss 'GMT'Z yyyy");
    System.out.println(dateFormat.format(cal.getTime()));

回答by Lukas Knuth

  1. Set the calendars data set(), add(), and roll().
  2. Use the Calendars getTime()-methodto get a Date-object.
  3. Use SimpleDateFormatto format the date like you want to.
  1. 设置日历数据set()add()roll()
  2. 使用 Calendars getTime()-method获取Date-object。
  3. 用于SimpleDateFormat根据需要格式化日期。

回答by rahulserver

Try:

尝试:

    final Calendar calendar = Calendar.getInstance();
    String dat=new SimpleDateFormat("EEE MMM dd hh:mm:ss 'GMT'Z yyyy").format(calendar.getTime())
    System.out.println(dat);//"Sat Jul 20 14:53:36 GMT+02:00 2013"

Should work for you!!

应该为你工作!!

回答by Venky

   call getTime() by using Calendar reference

    Calendar calendar = Calendar.getInstance();
    tv.setText(calendar.getTime() + "");

     **or**

Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat(
                "EEE MMM dd hh:mm:ss 'GMT'Z yyyy");
tv.setText(dateFormat.format(calendar.getTime()));