首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将日期和时间转换为milliSeconds

如何将日期和时间转换为milliSeconds
EN

Stack Overflow用户
提问于 2016-01-22 19:36:08
回答 2查看 706关注 0票数 0

我已经完成了获取和设置日期和时间到我的TextView,现在我想知道“如何转换日期和时间到milliSeconds当我选择和设置从DatePicker和TimePicker到TextView",帮助我与代码

我尝试过的代码,

代码语言:javascript
复制
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 ();
        }
    });
}}
EN

回答 2

Stack Overflow用户

发布于 2016-01-23 15:14:53

您可以尝试使用以下代码来获取日期的毫秒数。

代码语言:javascript
复制
  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,它提供了与date,time.using,我也有一个从DateTime返回毫秒的代码片段。

代码语言:javascript
复制
    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);

我希望你明白我的想法。

最幸运的

票数 1
EN

Stack Overflow用户

发布于 2016-01-22 19:38:17

像这样做,

代码语言:javascript
复制
  public String getDay(String str){
    if(str.equalsIgnoreCase("mon")
    return "Monday";
    ....
    }

根据您的代码

代码语言:javascript
复制
String mDate = dayFormat.format (cDay);
            StringBuilder dateBuilder = new StringBuilder ()
                    .append (getDay(mDate)).append (",")
                    .append (mMonth).append (" ")
                    .append (cDay).append (",")
                    .append (cYear);

代码语言:javascript
复制
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMM dd, YYYY");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34945821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档