I store current time in database each time application starts by user.
每当用户启动应用程序时,我将当前时间存储在数据库中。
Calendar c = Calendar.getInstance();
String str = c.getTime().toString();
Log.i("Current time", str);
In database side, I store current time as string (as you see in above code). Therefore, when I load it from database, I need to cast it to Date object. I saw some samples that all of them had used "DateFormat". But my format is exactly as same as Date format. So, I think there is no need to use "DateFormat". Am I right?
在数据库方面,我将当前时间存储为字符串(如上面的代码所示)。因此,当我从数据库加载它时,我需要将其强制转换为Date对象。我看到一些样本,他们都使用了“DateFormat”。但我的格式与日期格式完全相同。所以,我认为没有必要使用“DateFormat”。我对吗?
Is there anyway to directly cast this String to Date object? I want to compare this stored time with current time.
无论如何直接将此String转换为Date对象?我想将此存储时间与当前时间进行比较。
Thanks
谢谢
======> update
======>更新
Thanks dear guys. I used following code:
谢谢亲爱的伙计们。我使用以下代码:
private boolean isPackageExpired(String date){
boolean isExpired=false;
Date expiredDate = stringToDate(date, "EEE MMM d HH:mm:ss zz yyyy");
if (new Date().after(expiredDate)) isExpired=true;
return isExpired;
}
private Date stringToDate(String aDate,String aFormat) {
if(aDate==null) return null;
ParsePosition pos = new ParsePosition(0);
SimpleDateFormat simpledateformat = new SimpleDateFormat(aFormat);
Date stringDate = simpledateformat.parse(aDate, pos);
return stringDate;
}
6 个解决方案
#1
281
From String to Date
从字符串到日期
String dtStart = "2010-10-15T09:27:37Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
From Date to String
从日期到字符串
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = new Date();
String dateTime = dateFormat.format(date);
System.out.println("Current Date Time : " + dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
#2
7
SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY");
Date d = dateFormat.parse(datestring)
#3
5
using SimpleDateFormat or DateFormat class through
通过使用SimpleDateFormat或DateFormat类
for e.g.
例如
try{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // here set the pattern as you date in string was containing like date/month/year
Date d = sdf.parse("20/12/2011");
}catch(ParseException ex){
// handle parsing exception if date string was different from the pattern applying into the SimpleDateFormat contructor
}
#4
1
It could be a good idea to be careful with the Locale upon which c.getTime().toString();
depends.
小心使用c.getTime()。toString();的语言环境可能是个好主意。依靠。
One idea is to store the time in seconds (e.g. UNIX time). As an int
you can easily compare it, and then you just convert it to string when displaying it to the user.
一种想法是以秒为单位存储时间(例如,UNIX时间)。作为一个int,您可以轻松地比较它,然后在将其显示给用户时将其转换为字符串。
#5
0
String source = "24/10/17";
String[] sourceSplit= source.split("/");
int anno= Integer.parseInt(sourceSplit[2]);
int mese= Integer.parseInt(sourceSplit[1]);
int giorno= Integer.parseInt(sourceSplit[0]);
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(anno,mese-1,giorno);
Date data1= calendar.getTime();
SimpleDateFormat myFormat = new SimpleDateFormat("20yy-MM-dd");
String dayFormatted= myFormat.format(data1);
System.out.println("data formattata,-->"+dayFormatted);
#6
0
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyClass
{
public static void main(String args[])
{
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
String dateInString = "Wed Mar 14 15:30:00 EET 2018";
SimpleDateFormat formatterOut = new SimpleDateFormat("dd MMM yyyy");
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatterOut.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
here is your Date object date and the output is :
这是你的Date对象日期,输出是:
Wed Mar 14 13:30:00 UTC 2018
Wed Mar 14 13:30:00 UTC 2018
14 Mar 2018
2018年3月14日
#1
281
From String to Date
从字符串到日期
String dtStart = "2010-10-15T09:27:37Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
From Date to String
从日期到字符串
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = new Date();
String dateTime = dateFormat.format(date);
System.out.println("Current Date Time : " + dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
#2
7
SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY");
Date d = dateFormat.parse(datestring)
#3
5
using SimpleDateFormat or DateFormat class through
通过使用SimpleDateFormat或DateFormat类
for e.g.
例如
try{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // here set the pattern as you date in string was containing like date/month/year
Date d = sdf.parse("20/12/2011");
}catch(ParseException ex){
// handle parsing exception if date string was different from the pattern applying into the SimpleDateFormat contructor
}
#4
1
It could be a good idea to be careful with the Locale upon which c.getTime().toString();
depends.
小心使用c.getTime()。toString();的语言环境可能是个好主意。依靠。
One idea is to store the time in seconds (e.g. UNIX time). As an int
you can easily compare it, and then you just convert it to string when displaying it to the user.
一种想法是以秒为单位存储时间(例如,UNIX时间)。作为一个int,您可以轻松地比较它,然后在将其显示给用户时将其转换为字符串。
#5
0
String source = "24/10/17";
String[] sourceSplit= source.split("/");
int anno= Integer.parseInt(sourceSplit[2]);
int mese= Integer.parseInt(sourceSplit[1]);
int giorno= Integer.parseInt(sourceSplit[0]);
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(anno,mese-1,giorno);
Date data1= calendar.getTime();
SimpleDateFormat myFormat = new SimpleDateFormat("20yy-MM-dd");
String dayFormatted= myFormat.format(data1);
System.out.println("data formattata,-->"+dayFormatted);
#6
0
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyClass
{
public static void main(String args[])
{
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
String dateInString = "Wed Mar 14 15:30:00 EET 2018";
SimpleDateFormat formatterOut = new SimpleDateFormat("dd MMM yyyy");
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatterOut.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
here is your Date object date and the output is :
这是你的Date对象日期,输出是:
Wed Mar 14 13:30:00 UTC 2018
Wed Mar 14 13:30:00 UTC 2018
14 Mar 2018
2018年3月14日