当我按下按钮时如何获取时间并设置为文本字段
When i press a button how to get time at that time and set to a text field
我正在尝试创建聊天应用程序(移动应用程序)。当我按下发送按钮时,我需要将时间设置为 textview
您可以尝试以下操作:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
YOUR_TEXT_VIEW.setText(sdf.format(new Date(System.currentTimeMillis()));
您只需从系统获取当前日期和时间,然后显示在您的文本视图上。试试这个 -
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
your_textView.setText(dateFormat.format(new Date());
你可以试试这个,它可能对你有帮助
Calendar calendar = Calendar.getInstance();
int a = calendar.get(Calendar.AM_PM);
String time;
if(a == Calendar.AM) {
time = calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+"AM";
}
else {
time = calendar.get(Calendar.HOUR)+ ":"+calendar.get(Calendar.MINUTE)+"PM";
}
textView.setText(time);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat(" d MMM yyyy HH:mm:ss ");
String time = format.format(calendar.getTime());
TextView textView = findViewById(R.id.cdt);
textView.setText(time);
});
}
}
我正在尝试创建聊天应用程序(移动应用程序)。当我按下发送按钮时,我需要将时间设置为 textview
您可以尝试以下操作:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
YOUR_TEXT_VIEW.setText(sdf.format(new Date(System.currentTimeMillis()));
您只需从系统获取当前日期和时间,然后显示在您的文本视图上。试试这个 -
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
your_textView.setText(dateFormat.format(new Date());
你可以试试这个,它可能对你有帮助
Calendar calendar = Calendar.getInstance();
int a = calendar.get(Calendar.AM_PM);
String time;
if(a == Calendar.AM) {
time = calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+"AM";
}
else {
time = calendar.get(Calendar.HOUR)+ ":"+calendar.get(Calendar.MINUTE)+"PM";
}
textView.setText(time);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat(" d MMM yyyy HH:mm:ss ");
String time = format.format(calendar.getTime());
TextView textView = findViewById(R.id.cdt);
textView.setText(time);
});
}
}