Android toString()返回日期对象上意外的内容
我最近开始使用AndroidStudio 3.1.2和SDK 19编写我的第一个Android项目 我的一个对象具有日期属性。有时我想在文本视图中显示整个日期时间或部分日期时间。所以我尝试了新手的方式,并在约会时调用了toString()。 但是,显示的文本包含我在用于创建日期对象的SingleDateFormat模式中没有定义的元素 以下是我在myObject上创建日期的方式:Android toString()返回日期对象上意外的内容,android,date,tostring,Android,Date,Tostring,我最近开始使用AndroidStudio 3.1.2和SDK 19编写我的第一个Android项目 我的一个对象具有日期属性。有时我想在文本视图中显示整个日期时间或部分日期时间。所以我尝试了新手的方式,并在约会时调用了toString()。 但是,显示的文本包含我在用于创建日期对象的SingleDateFormat模式中没有定义的元素 以下是我在myObject上创建日期的方式: Date date1; Date date2; SimpleDateFormat format = new Sim
Date date1;
Date date2;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date1 = format.parse(json.getString("date_1"));
dtae2 = format.parse(json.getString("date_2"));
} catch(ParseException e) {
//error handling stuff
e.printStackTrace();
}
这是我要在视图上显示日期的位置:
myTextView.setText("First appearance logged at " + myObject.getDate1().toString());
我希望显示一个类似2018-08-16 12:14:42
的字符串。相反,我得到的是周四8月12:14:42 GMT+2018年02:00
。这似乎是另一种日期格式,忽略了我的自定义模式
所以我的问题是,如果有一种方法可以操纵toString()的输出,那么日期就会按照我在模式中定义的方式显示。我可以把模式传递给toString()方法吗
编辑
我将对象的属性更改为字符串类型,尽管这样更便于显示。将它们转换为日期的原因是,我需要计算两个人之间的持续时间,但这不是我无法解决的问题。感谢社区。您需要使用类似以下内容的SimpleDataFormat:
String myFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
myTextView.setText("First appearance logged at " + sdf.format(date1));
您需要使用类似以下内容的SimpleDataFormat:
String myFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
myTextView.setText("First appearance logged at " + sdf.format(date1));
根据您的需要,您可以使用json.getString(“date\u 1”)
。
您不需要设置额外的逻辑。要将字符串
日期转换为日期
对象进行某些计算时,需要进行解析
如果要更改接收日期的格式,请使用此方法
changeStringDateFormat(json.getString("date_1"), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd");
只需将此方法放在Util中
public String changeStringDateFormat(String date, String inputDateFormat, String outPutDateFormat) {
Date initDate = null;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(inputDateFormat);
initDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outputFormatter = new SimpleDateFormat(outPutDateFormat);
String parsedDate = outputFormatter.format(initDate);
return parsedDate;
}
请参见,它从默认格式返回字符串
公共字符串toString()
将此日期对象转换为以下格式的字符串:
道指周一至dd hh:mm:ss zzz yyyy
根据您的需要,您可以使用json.getString(“date\u 1”)
。
您不需要设置额外的逻辑。要将字符串
日期转换为日期
对象进行某些计算时,需要进行解析
如果要更改接收日期的格式,请使用此方法
changeStringDateFormat(json.getString("date_1"), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd");
只需将此方法放在Util中
public String changeStringDateFormat(String date, String inputDateFormat, String outPutDateFormat) {
Date initDate = null;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(inputDateFormat);
initDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outputFormatter = new SimpleDateFormat(outPutDateFormat);
String parsedDate = outputFormatter.format(initDate);
return parsedDate;
}
请参见,它从默认格式返回字符串
公共字符串toString()
将此日期对象转换为以下格式的字符串:
道指周一至dd hh:mm:ss zzz yyyy
只需编写这个代码片段 JAVA文件
public class MainActivity extends AppCompatActivity {
TextView my_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_text = findViewById(R.id.my_text);
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
Toast.makeText(getApplicationContext(), "" + date, Toast.LENGTH_SHORT).show();
my_text.setText("Your Date is : " + date);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mydemo.com.anew.MainActivity">
<TextView
android:id="@+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
XML文件
public class MainActivity extends AppCompatActivity {
TextView my_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_text = findViewById(R.id.my_text);
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
Toast.makeText(getApplicationContext(), "" + date, Toast.LENGTH_SHORT).show();
my_text.setText("Your Date is : " + date);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mydemo.com.anew.MainActivity">
<TextView
android:id="@+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
查看与您的需求相同的输出截图获取当前日期:
参考此
希望这对您有所帮助只需编写此代码片段即可
JAVA文件
public class MainActivity extends AppCompatActivity {
TextView my_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_text = findViewById(R.id.my_text);
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
Toast.makeText(getApplicationContext(), "" + date, Toast.LENGTH_SHORT).show();
my_text.setText("Your Date is : " + date);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mydemo.com.anew.MainActivity">
<TextView
android:id="@+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
XML文件
public class MainActivity extends AppCompatActivity {
TextView my_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_text = findViewById(R.id.my_text);
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
Toast.makeText(getApplicationContext(), "" + date, Toast.LENGTH_SHORT).show();
my_text.setText("Your Date is : " + date);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mydemo.com.anew.MainActivity">
<TextView
android:id="@+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
查看与您的需求相同的输出截图获取当前日期:
参考此
希望这对您有所帮助在我的项目中,我一直在使用format()
函数格式化,如下所示:
myTextView.setText(“第一次出现记录在”+format.format(myObject.getData1()))代码>
我希望这能有所帮助。在我的项目中,我一直在使用format()
函数格式化,如下所示:
myTextView.setText(“第一次出现记录在”+format.format(myObject.getData1()))代码>
我希望这能有所帮助。tl;博士
使用现代java.time类,而不是可怕的遗留类Date
&SimpleDateFormat
类
myJavaUtilDate // Never use `java.util.Date`.
.toInstant() // Convert from legacy class to modern replacement. Returns a `Instant` object, a moment in UTC.
.atOffset( // Convert from the basic `Instant` class to the more flexible `OffsetDateTime` class.
ZoneOffset.UTC // Constant defining an offset-from-UTC of zero, UTC itself.
) // Returns a `OffsetDateTime` object.
.format( // Generate a `String` with text representing the value of this `OffsetDateTime` object.
DateTimeFormatter.ISO_LOCAL_DATE_TIME // Pre-defined formatter stored in this constant.
) // Returns a `String` object.
.replace( "T" , " " ) // Replace the standard `T` in the middle with your desired SPACE character.
2018-08-16 10:14:42
java.time
您使用的是几年前被java.time类取代的糟糕的旧类
如果交给java.util.Date
对象,立即转换为java.time.Instant
。两者都代表UTC中的一个时刻Instant
的分辨率为纳秒,而不是毫秒
要在传统类和现代类之间转换,请查看添加到旧类的新转换方法
Instant
ISO 8601
要生成与所需格式类似的标准格式文本的字符串
,请调用toString
String output = instant.toString() ; // Generate text in standard ISO 8601 format.
运行时间=持续时间
顺便说一下,要计算经过的时间,请使用Duration
类。传递一对Instant
对象以计算经过的24小时“天”、小时、分钟和秒数
Duration d = Duration.between( start , stop ) ; // Calc elapsed time.
2018-08-16T10:14:42Z
OffsetDateTime
对于其他格式,请从基本的Instant
类转换为更灵活的OffsetDateTime
类
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
odt.toString():2018-08-16T10:14:42Z
DateTimeFormatter
您所需的格式接近预定义的格式设置程序。只需在中间用空间替换<代码> T >代码>
String output = odt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME )
.replace( "T" , " " ) ;
2018-08-16 10:14:42
ZoneDateTime
请记住,我们目前只关注UTC。在任何一个特定的时刻,一天中的日期和时间在全球各地都因地区而异
如果您想通过某个地区(时区)的人们使用的挂钟时间镜头看到同一时刻,请应用ZoneId
以获得zoneDateTime
对象
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
zdt.toString():2018-08-16T11:14:42+01:00[非洲/突尼斯]
您可以使用如上所示的格式设置程序以所需格式生成字符串
String output = zdt.format( f ) ;
关于java.time
该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&
该项目现已启动,建议迁移到类
要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是
您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,不需要