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);
Related
I am working on a App in which i want to display notification time.
I can display notification time but not able to add time zone in it.
My current location is Pakistan and i want to add GMT+5:00
My code is attached
String currentDateTimeString = DateFormat.getTimeInstance().format(notif.At);
textViewTime.setText(currentDateTimeString);
in this code, notif.At is dateTime variable. I also attached screenshot of my app, i want to ask you , how to add timeZone value in notif.At. Thanks!
Update
You mark time with timezone in order to solve internationalization problem, I understand, right?
If so, I think it could be better to convert your date to UTC date. When you change to another timezone, just convert this UTC Date to local.
public static Date localToUtc(Date localDate) {
return new Date(localDate.getTime()-TimeZone.getDefault().getOffset(localDate.getTime()));
}
public static Date utcToLocal(Date utcDate) {
return new Date(utcDate.getTime()+TimeZone.getDefault().getOffset(utcDate.getTime()));
}
Old answer
If your notif.At is Dateobject, it's a same question actually:
TimeZone tz = TimeZone.getDefault();
Date date = new Date();
final String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
String result = sdf.format(date);
Log.d("Date ", "date: " + result + " " + tz.getDisplayName(false, TimeZone.SHORT));
print:
date: 2015-03-31 18:45:28 GMT+08:00
You can try java.time api;
Instant date = Instant.ofEpochMilli(1549362600000l);
LocalDateTime utc = LocalDateTime.ofInstant(date, ZoneOffset.UTC);
LocalDateTime pst = LocalDateTime.ofInstant(date, ZoneOffset.of("+05:00"));
LocalDateTime is = LocalDateTime.ofInstant(date, ZoneOffset.of("+05:30"));
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()));
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 must convert the actual date and time to millis and into other timezone GMT+3 (my timezone is GMT-2). I use this code but it return me hte time but into my timezone....why ?
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
cal.get(Calendar.DAY_OF_MONTH);
cal.get(Calendar.MONTH);
cal.get(Calendar.YEAR);
cal.get(Calendar.HOUR_OF_DAY);
cal.get(Calendar.MINUTE);
cal.get(Calendar.SECOND);
cal.get(Calendar.MILLISECOND);
long timez = cal.getTime().getTime();
You need to use SimpleDateFormat. Calendar always uses default configured timezone on your machine. Here is example on how to achieve this functionality with SimpleDateFormat.
Date date = new Date();
DateFormat firstFormat = new SimpleDateFormat();
DateFormat secondFormat = new SimpleDateFormat();
TimeZone firstTime = TimeZone.getTimeZone(args[0]);
TimeZone secondTime = TimeZone.getTimeZone(args[1]);
firstFormat.setTimeZone(firstTime);
secondFormat.setTimeZone(secondTime);
System.out.println("-->"+args[0]+": " + firstFormat.format(date));
System.out.println("-->"+args[1]+": " + secondFormat.format(date));
}
where arg[0] and arg1 are the two time zone.
Refer this LINK
The getTime() method return the same time. it has no relation with the timezone.
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());