This question already has answers here:
How to get current time and date in Android
(42 answers)
Closed 9 years ago.
I need to get the Current time in a string format in android. I have seen a toString method in Time class. Is there any way to get the current time using Time class or object in android? If not, how can I get the current time as a string in android?
That's how I do it:
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getBaseContext());
dateFormat.format(date);
There are multiple possibilities:
Automatically use the user-preferred format:
String dateString = DateFormat.getTimeFormat(thisContext).format(Calendar.getInstance().getTime())
Use your own format:
String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
try to do something like this
Time time = new Time();
System.out.println("time = " + time);
System.out.println("time.toHour() " + time.toHour());
// etc..
// Test with a supplied value
Time time2 = new Time(12033312L);
System.out.println("time2 = " + time2);
also
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
Related
This question already has answers here:
How can I get current date in Android?
(31 answers)
Closed 6 years ago.
I am using this code to get current date to string, however, the date it gets is 1899-12-31 how is this possible?
// save date
String date = new SimpleDateFormat("yyyy-MM-dd")
.format(new Date(0, 0, 0));
file.writeToSD("Date: " + date.toString());
0,0,0 is the epoc -- in this case the year 1900
try date();
better take a look at the date page
If you want to retrieve current date then use only use newDate(). Its return current millisecond
In your case you should use bellow code
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = dateFormat.format(new Date());
file.writeToSD("Date = " + currenDate);
I'm using Time.getCurrentTimezone()to get the current timezone and therefore the date.
I'm getting and formatting it like this:
private void setDate() {
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
tvdate2.setText(today.monthDay + "-" + today.month + "-" + today.year);
}
How can I format the date according to the system settings?
I didn't manage to get it done via SimpleDateFormat...
You could get time in any format by using SimpleDateFormat class. Please refer to this
Calendar cal = Calendar.getInstance();
String currentDate = cal.get(Calendar.DAY_OF_MONTH)+"/"+(cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.YEAR);
Date date = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.ENGLISH).parse(currentDate);
I am developing an application in that when time is 11:26 it is showing 11:07. I used Calendar instance to do.
Calendar currentDate=Calendar.getInstance();
SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
datenow=ddMMyyyy.format(currentDate.getTime());
if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MONTH)+":PM";
}else{
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MONTH)+":AM";
}
new MyToast(this, "Date = "+datenow+" time = "+timenow);
The out put is wrong what to do?
you are displaying month in place of minute. Change your code to my code
Calendar currentDate=Calendar.getInstance();
SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
String datenow = ddMMyyyy.format(currentDate.getTime());
String timenow;
if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":PM";
}else{
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":AM";
}
Toast.makeText(MainActivity.this, "Date = "+datenow+" time = "+timenow,Toast.LENGTH_SHORT).show();
11:07 is displaying because 07 ==> month number in a year. month will be calculated from 0 -> 11
You have mistaken see here Calendar.HOUR + : + Calendar.MONTH. It should be as follows,
Calendar currentDate=Calendar.getInstance();
SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
datenow=ddMMyyyy.format(currentDate.getTime());
if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":PM";
}else{
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":AM";
}
new MyToast(this, "Date = "+datenow+" time = "+timenow);
Instead of MINUTE you were accessing MONTH object of Calendar Class.
I have a requirement that I need to compare two Dates. One Date will come from DB which is String in "YYYY-DD-MM" firm and I need to compare this String Date with current Date.
for this I am converting Date String into Date object.
Now I need current Date also in "YYYY-MM-DD" format and it should be Date object so that I can use.compareTo() method compare two dates..
Please help me how to do that...
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
You can do it in following way
// pick current system date
Date dt = new Date();
// set format for date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// parse it like
String check = dateFormat.format(dt);
System.out.println("DATE TO FROM DATEBASE " +
arrayOfStringDate[d].toString());
System.out.println("CURRENT DATE " + check);
// and compare like
System.out.println("compare "+
arrayOfStringDate[d].toString().equals(check));
Calendar c = Calendar.getInstance();
SimpleDateFormat tf = new SimpleDateFormat("yyyy-MM-dd");
String time=DB time;
Date parseTime= tf.parse(time);
Integer dayNow=c.getTime().getDate();
Integer dayDb=parseTime.getDate();
then you can compare dayNow and dayDb.
If your current date is actually an instance of the java.util.Date class, you don't need to specify a format for it; it's just a millisecond value that represents a specific moment in time.
You can get the current date like so:
Date currentDate = new Date();
You can use 2 ways:
DateFormat object. Use parse method.
Make your own parser of the Date. I mean, you convert the year, month and day in an integer each, and use Date constructor to get the Date.
in my Android application, i am taking date and time from database. but i am not able to get the date in the "Date" Format from the database into my application, the date is in string format, so i am not able to compare the system date to database date.
if i convert the system date into string then i am not able to update the date into the database in recurring case. i also want to update the database date if the system date and database date is matched.
how can i achieve this is android.
Thanks in advance.
You can convert String to Date like this:
String str = "12/12/1912";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(str);
And back to String
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Date is : " + formatter.format(date));
And Date has before and after methods and can be compared to each other.
By the way there is also a library called Joda, you can also check it out.
Try this code:
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MMMM");
formattedDate = df.format(c.getTime());