如何根据用户设备上的时间和日期将两个值中的任何一个显示为 TextView?
How to display either of the two values as a TextView, based on the time and date on the user's device?
我正在为一家咖啡店创建一个应用程序,它应该显示关于它在给定时间内是关闭还是打开的信息。该应用程序应根据用户设备上的时间将“OPEN”或“CLOSED”显示为 TextView
。
咖啡店早上 10:00:00 开始营业,晚上 24:00:00 关门。此外,它从周三至周日开放(周一和周二关闭)。
为此,我在 XML 中创建了一个带有空文本字段的 TextView
布局:
<TextView
android:id="@+id/status_text_view"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
在 java 文件中,我创建了一个 TextView
元素并引用了 XML
文件中的 TextView
布局:
TextView textView = (TextView) findViewById(R.id.status_text_view);
我还使用以下方法从用户设备获取日期和时间:
String currentDateTimeString = java.text.DateFormat.getDateTimeInstance().format(new Date());
从这里我不知道如何根据用户设备上的日期、时间和日期在 TextView
布局中显示“打开”或“关闭”的最终任务。我能做的只是使用代码在 TextView
布局中显示当前日期和时间:
textView.setText(currentDateTimeString);
注意:如果您分享任何代码,请添加描述每一行代码的注释,因为我是初级程序员。
您可以创建一个 if...else 条件来设置该 TextView。
这是我认为会有所帮助的示例。
if((currentTime > openTime) && (currentTime < closeTime)) {
if((currentDay != dayMonday) && (currentDay != dayTuesday)) {
textView.setText("OPEN");
}
} else {
textView.setText("CLOSED");
}
第一个 IF 语句 (if((currentTime > openTime) && (currentTime < closeTime))) 确保只有当 currentTime 变量在 1000hrs 到 2400hrs 之间时语句 returns TRUE。
第二个 IF 语句 (if((currentDay != dayMonday) && (currentDay != dayTuesday))) 是确保它 return 只有当 currentDay 不是星期一或星期二时才为真。
并且,您的 currentDateTimeString 一起给出日期和当前时间。所以你必须根据 currentDay 和 currentTime 解析它或者从 Calendar class documentation.
中找到合适的常量
祝你好运。希望对你有帮助
Java 8 介绍了java.time.*
包应该首选!您可以在 Java SE 8 Date and Time
中找到更多信息
您应该考虑使用 java.time.LocalDateTime
。
以下函数给出了一个简单的例子来判断您的店铺是否营业:
import java.time.DayOfWeek;
import java.time.LocalDateTime;
boolean isOpen() {
LocalDateTime now = LocalDateTime.now();
// now.getHour/( returns the hour-of-day, from 0 to 23
// so every hour that is greater equals 10 means open
boolean isOpenByTime = now.getHour() >= 10;
DayOfWeek dayOfWeek = now.getDayOfWeek();
boolean isOpenByWeekDay = dayOfWeek != DayOfWeek.MONDAY && dayOfWeek != DayOfWeek.TUESDAY;
return isOpenByTime && isOpenByWeekDay;
}
然后您可以像这样使用 isOpen()
:
if(isOpen()) { textView.setText("OPEN"); } else { textView.setText("CLOSED"); }
文档
- Whosebug: Java: getMinutes and getHours
我在 JAVA 编译器中编写了您的解决方案,但逻辑仍然适用于 android
只需执行函数 checkIfClosed()、getDay() 和 currentTime().
import java.util.*;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
public class checkIfclosed {
public static void main(String[] args) throws IOException {
try {
System.out.println(checkIfClosed());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String checkIfClosed() throws ParseException {
String out = "";
if(!(getDay().equals("MONDAY") || getDay().equals("TUESDAY"))){
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
if(dateFormat.parse(currentTime()).after(dateFormat.parse("10:00:00"))
&& dateFormat.parse(currentTime()).before(dateFormat.parse("24:00:00"))){
out = "OPEN";
}
else{
out = "CLOSED";
}
}
else{
out = "CLOSED";
}
return out;
}
private static String getDay(){
String ox = "";
LocalDate localDate = LocalDate.now();
DayOfWeek dayOfWeek = DayOfWeek.from(localDate);
ox = dayOfWeek.name();
return ox;
}
private static String currentTime(){
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
return dtf.format(now);
}
}
Android Code
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Log.v("isClosed", checkIfClosed());
} catch (ParseException e) {
e.printStackTrace();
}
}
private static String checkIfClosed() throws ParseException {
String out = "";
if(!(getDay().equals("MONDAY") || getDay().equals("TUESDAY"))){
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
if(dateFormat.parse(currentTime()).after(dateFormat.parse("10:00:00"))
&& dateFormat.parse(currentTime()).before(dateFormat.parse("24:00:00"))){
out = "OPEN";
}
else{
out = "CLOSED";
}
}
else{
out = "CLOSED";
}
return out;
}
private static String getDay(){
String ox = "";
LocalDate localDate = LocalDate.now();
DayOfWeek dayOfWeek = DayOfWeek.from(localDate);
ox = dayOfWeek.name();
return ox;
}
private static String currentTime(){
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
return dtf.format(now);
}
}
我正在为一家咖啡店创建一个应用程序,它应该显示关于它在给定时间内是关闭还是打开的信息。该应用程序应根据用户设备上的时间将“OPEN”或“CLOSED”显示为 TextView
。
咖啡店早上 10:00:00 开始营业,晚上 24:00:00 关门。此外,它从周三至周日开放(周一和周二关闭)。
为此,我在 XML 中创建了一个带有空文本字段的 TextView
布局:
<TextView
android:id="@+id/status_text_view"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
在 java 文件中,我创建了一个 TextView
元素并引用了 XML
文件中的 TextView
布局:
TextView textView = (TextView) findViewById(R.id.status_text_view);
我还使用以下方法从用户设备获取日期和时间:
String currentDateTimeString = java.text.DateFormat.getDateTimeInstance().format(new Date());
从这里我不知道如何根据用户设备上的日期、时间和日期在 TextView
布局中显示“打开”或“关闭”的最终任务。我能做的只是使用代码在 TextView
布局中显示当前日期和时间:
textView.setText(currentDateTimeString);
注意:如果您分享任何代码,请添加描述每一行代码的注释,因为我是初级程序员。
您可以创建一个 if...else 条件来设置该 TextView。
这是我认为会有所帮助的示例。
if((currentTime > openTime) && (currentTime < closeTime)) {
if((currentDay != dayMonday) && (currentDay != dayTuesday)) {
textView.setText("OPEN");
}
} else {
textView.setText("CLOSED");
}
第一个 IF 语句 (if((currentTime > openTime) && (currentTime < closeTime))) 确保只有当 currentTime 变量在 1000hrs 到 2400hrs 之间时语句 returns TRUE。
第二个 IF 语句 (if((currentDay != dayMonday) && (currentDay != dayTuesday))) 是确保它 return 只有当 currentDay 不是星期一或星期二时才为真。
并且,您的 currentDateTimeString 一起给出日期和当前时间。所以你必须根据 currentDay 和 currentTime 解析它或者从 Calendar class documentation.
中找到合适的常量祝你好运。希望对你有帮助
Java 8 介绍了java.time.*
包应该首选!您可以在 Java SE 8 Date and Time
您应该考虑使用 java.time.LocalDateTime
。
以下函数给出了一个简单的例子来判断您的店铺是否营业:
import java.time.DayOfWeek;
import java.time.LocalDateTime;
boolean isOpen() {
LocalDateTime now = LocalDateTime.now();
// now.getHour/( returns the hour-of-day, from 0 to 23
// so every hour that is greater equals 10 means open
boolean isOpenByTime = now.getHour() >= 10;
DayOfWeek dayOfWeek = now.getDayOfWeek();
boolean isOpenByWeekDay = dayOfWeek != DayOfWeek.MONDAY && dayOfWeek != DayOfWeek.TUESDAY;
return isOpenByTime && isOpenByWeekDay;
}
然后您可以像这样使用 isOpen()
:
if(isOpen()) { textView.setText("OPEN"); } else { textView.setText("CLOSED"); }
文档
- Whosebug: Java: getMinutes and getHours
我在 JAVA 编译器中编写了您的解决方案,但逻辑仍然适用于 android 只需执行函数 checkIfClosed()、getDay() 和 currentTime().
import java.util.*;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
public class checkIfclosed {
public static void main(String[] args) throws IOException {
try {
System.out.println(checkIfClosed());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String checkIfClosed() throws ParseException {
String out = "";
if(!(getDay().equals("MONDAY") || getDay().equals("TUESDAY"))){
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
if(dateFormat.parse(currentTime()).after(dateFormat.parse("10:00:00"))
&& dateFormat.parse(currentTime()).before(dateFormat.parse("24:00:00"))){
out = "OPEN";
}
else{
out = "CLOSED";
}
}
else{
out = "CLOSED";
}
return out;
}
private static String getDay(){
String ox = "";
LocalDate localDate = LocalDate.now();
DayOfWeek dayOfWeek = DayOfWeek.from(localDate);
ox = dayOfWeek.name();
return ox;
}
private static String currentTime(){
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
return dtf.format(now);
}
}
Android Code
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Log.v("isClosed", checkIfClosed());
} catch (ParseException e) {
e.printStackTrace();
}
}
private static String checkIfClosed() throws ParseException {
String out = "";
if(!(getDay().equals("MONDAY") || getDay().equals("TUESDAY"))){
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
if(dateFormat.parse(currentTime()).after(dateFormat.parse("10:00:00"))
&& dateFormat.parse(currentTime()).before(dateFormat.parse("24:00:00"))){
out = "OPEN";
}
else{
out = "CLOSED";
}
}
else{
out = "CLOSED";
}
return out;
}
private static String getDay(){
String ox = "";
LocalDate localDate = LocalDate.now();
DayOfWeek dayOfWeek = DayOfWeek.from(localDate);
ox = dayOfWeek.name();
return ox;
}
private static String currentTime(){
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
return dtf.format(now);
}
}