【问题标题】:How to convert Date and time to milliSeconds如何将日期和时间转换为毫秒
【发布时间】:2016-01-22 11:36:07
【问题描述】:

我已经完成了获取和设置我的 TextView 的日期和时间,现在我想知道“当我从 DatePicker 和 TimePicker 选择并设置为 TextView 时如何将日期和时间转换为毫秒”,帮我编写代码

我尝试过的代码,

public class MainActivity extends AppCompatActivity {
private TextView txtDateSet;
private TextView txtTimeSet;
int cDay, cMonth, cYear;
int tHours, tMinutes;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_main);

    txtDateSet = (TextView) findViewById (R.id.txt_date_display);
    txtTimeSet = (TextView) findViewById (R.id.txt_time_display);

    txtDateSet.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick (View v) {
            displayAlertDialog ();
        }
    });

}

public void displayAlertDialog () {

    AlertDialog.Builder builder = new AlertDialog.Builder (this);
    LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate (R.layout.custom_alert, (ViewGroup) findViewById (R.id.lnt_root));
    final DatePicker dateSet = (DatePicker) layout.findViewById (R.id.date_picker);
    final TimePicker timeSet = (TimePicker) layout.findViewById (R.id.time_picker);
    final TextView txtOk = (TextView) layout.findViewById (R.id.txt_ok);

    builder.setView (layout);
    final AlertDialog alertDialog = builder.create ();
    alertDialog.setCanceledOnTouchOutside (true);
    alertDialog.show ();

    txtOk.setOnClickListener (new View.OnClickListener () {
        @TargetApi(Build.VERSION_CODES.M)
        @Override
        public void onClick (View v) {
            cDay = dateSet.getDayOfMonth ();
            cMonth = dateSet.getMonth ();
            cYear = dateSet.getYear ();
            tHours = timeSet.getCurrentHour ();
            tMinutes = timeSet.getCurrentMinute ();

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, cYear);
            cal.set(Calendar.MONTH, cMonth);
            cal.set (Calendar.DAY_OF_MONTH, cDay);
            cal.set (Calendar.HOUR_OF_DAY, tHours);
            cal.set (Calendar.MINUTE, tMinutes);

            SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy");
            String formatedDate = sdf.format(cal.getTime ());
            txtDateSet.setText (""+formatedDate);

            SimpleDateFormat stf = new SimpleDateFormat ("hh:mm aa");
            String formatedTime = stf.format (cal.getTime ());
            txtTimeSet.setText (""+formatedTime);

            alertDialog.dismiss ();
        }
    });
}}

【问题讨论】:

    标签: android datepicker timepicker


    【解决方案1】:

    您可以尝试按照代码从日期获取毫秒数。

      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
                format.setTimeZone(TimeZone.getTimeZone("UTC"));
                Date date = format.parse(text);
                long millis = date.getTime();
    

    我们还有一个标准库,称为JodaTime,它提供了与日期时间相关的 Lot 关闭功能。使用这个库,您可以轻松地从 DateTime 获取毫秒。 我也有一个代码片段,它只是从 DateTime 返回毫秒。

        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
                        .withLocale(Locale.US)
                        .withZoneUTC();
    
    
                DateTime datetime = formatter.parseDateTime(text);
                long millis = dateTime.getMillis();
    
                //Or
                //long millis = formatter.parseMillis(text);
    

    我希望你对我的想法很清楚。

    祝你好运

    【讨论】:

      【解决方案2】:

      这样做,

        public String getDay(String str){
          if(str.equalsIgnoreCase("mon")
          return "Monday";
          ....
          }
      

      根据您的代码

      String mDate = dayFormat.format (cDay);
                  StringBuilder dateBuilder = new StringBuilder ()
                          .append (getDay(mDate)).append (",")
                          .append (mMonth).append (" ")
                          .append (cDay).append (",")
                          .append (cYear);
      

      SimpleDateFormat format = new SimpleDateFormat("EEEE, MMM dd, YYYY");
      

      【讨论】:

        猜你喜欢
        • 2017-02-14
        • 1970-01-01
        • 2014-11-26
        • 1970-01-01
        • 1970-01-01
        • 2021-06-14
        • 2019-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多