【问题标题】:How to get Date from a String in android?如何从android中的字符串获取日期?
【发布时间】:2016-12-09 13:02:37
【问题描述】:

我想从给定的字符串中获取日期。假设我们有一个字符串 09-Dec-2016 12:00:00 AM 我只想显示 30-Dec-2016

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView=(TextView)findViewById(R.id.date);

       // String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
        String strCurrentDate = "30-Dec-2016 12:00:00 AM";
       // SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
        SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
        Date newDate = null;
        try {
            newDate = format.parse(strCurrentDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        format = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
        String date = format.format(newDate);
        textView.setText(date);


    }

我想显示这样的日期 2016 年 12 月 30 日

【问题讨论】:

  • 有谁能帮忙吗?
  • 日期从 9 日更改为 30 日。那是一段时间旅行。
  • 什么意思?
  • 阅读你的问题......然后你就知道AlphaQ是什么意思......
  • 用 AlphaQ 改了

标签: android android-studio


【解决方案1】:

这应该可行。

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
Date date = sdf.parse(inputDateString);
sdf = new SimpleDateFormat("dd-MMM-yyyy");
String outputString  = sdf.format(date);

【讨论】:

  • 当想要将它提供给获取 nullpointerException 的 textview 时。没有解决我的问题,先生。
  • 检查你的textview是否为null或者日期字符串是否为null,并处理空指针异常。同时将解析异常添加到日期并处理。
【解决方案2】:

这样做:

String currDate = "09-Dec-2016 12:00:00 AM";
String date = currDate.split("\\s")[0];

【讨论】:

    【解决方案3】:
    public Date String_date(String date) 
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk-mm");
        Date myDate = null;
        try {
            myDate = dateFormat.parse(date);
    
            return myDate;
    
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return myDate;
    
    }
    

    【讨论】:

      【解决方案4】:

      试试这个:

          String strCurrentDate = "30-Dec-2016 12:00:00 AM";
          SimpleDateFormat formatParse = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a",Locale.getDefault());
          Date newDate = null;
          try {
              newDate = formatParse.parse(strCurrentDate);
          } catch (ParseException e) {
              e.printStackTrace();
          }
      
          SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
          String date = format.format(newDate);
          textView.setText(date);
      

      ps。 formats eg.

      【讨论】:

        【解决方案5】:

        //解析 Date từ Date sang String SimpleDateFormat 格式 = new SimpleDateFormat("dd-MM-yyyy");

                Date newdate = new Date(Long.parseLong(tt.getThoiGian()));
        
        
        
                format = new SimpleDateFormat("dd-MM-yyyy");
                String date1 = format.format(newdate);
                txtThoiGian.setText(date1);
        

        【讨论】:

        • SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy") format = new SimpleDateFormat("dd-MM-yyyy");字符串 date1 = format.format(newdate); txtThoiGian.setText(date1);
        • 在回答一个老问题时,如果您包含一些上下文来解释您的答案如何提供帮助,那么您的答案对其他 StackOverflow 用户会更有用。另外,请将您的评论编辑到您的问题中。请参阅:How do I write a good answer
        猜你喜欢
        • 2015-05-13
        • 2019-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多