Android Date To Milles (ISO 8601) - android

am working on countdown widget .The problem is explained below
'2012-07-04T15:00:00Z' - > '1341414000000'
'1341414000000' - > indicate 2012 july 4th 20:30
why this happend? . Am using joda
final String format = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
DateTime endTime = formatter.parseDateTime(strDate);
long diff=endTime.getMillis();

String time="2012-07-04T15:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
// time.replace("Z","");
try {
Date date=df.parse(time);
long diff=date.getTime()-System.currentTimeMillis();
System.out.println("Date "+diff);
} catch (ParseException e) {
e.printStackTrace();
}

This seems like an old question, but I'll answer it anyway, since other people may find this.
In Joda there is a class for ISO 8601 formats, so instead of specifying the format manually you could use that class, as follows:
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.DateTime;
String strDate = "2012-07-04T15:00:00Z";
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime endTime = formatter.parseDateTime(strDate);
long diff=endTime.getMillis();
On the other hand, the problem that you seem to be having is related to the time zone. When you convert back from millis to date string, it gets converted using the local time zone. If you want to get the date as UTC, you should do the following:
import org.joda.time.DateTimeZone;
import org.joda.time.DateTime;
DateTime dt = new DateTime(1341414000000).withZone(DateTimeZone.UTC);
It will return 2012-07-04T15:00:00.000Z as expected. If you want to format it without the milliseconds, you can use the same formatter as before:
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.DateTime;
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime dt = new DateTime(1341414000000).withZone(DateTimeZone.UTC);
formatter.print(dt)
And it will return 2012-07-04T15:00:00Z.

Related

How to change the language of the month to Spanish?

I receive the date from API like this:
2020-09-10T20:00:00.000Z
when I convert this date, It shows SEPTEMBER 10, 2020 8:00 p. m.
I need show the month in Spanish, e.g Septiembre or Sep
I recommend you do it with the modern java.time date-time API and the corresponding formatting API (package, java.time.format) instead of with the outdated and error-prone java.util date-time API and SimpleDateFormat. Learn more about the modern date-time API from Trail: Date Time. If your Android API level is still not compliant with Java8, check How to use ThreeTenABP in Android Project and Java 8+ APIs available through desugaring.
Do it as follows using the modern date-time API:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// The given date-time string
String strDateTime = "2020-09-10T20:00:00.000Z";
// Parse the given date-time string into OffsetDateTime
OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
// Define the formatter for output in a custom pattern and in Spanish Locale
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd, uuuu hh:mm a", new Locale("es", "ES"));
// Print instant using the defined formatter
String formatted = formatter.format(odt);
System.out.println(formatted);
}
}
Output:
septiembre 10, 2020 08:00 p. m.
If you still want to use the legacy date-time and formatting API, you can do it as follows:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
// The given date-time string
String strDateTime = "2020-09-10T20:00:00.000Z";
// Define the formatter to parse the input string
SimpleDateFormat inputFormatter = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// Parse the given date-time string into java.util.Date
Date date = inputFormatter.parse(strDateTime);
// Define the formatter for output in a custom pattern and in Spanish Locale
SimpleDateFormat outputFormatter = new SimpleDateFormat("MMMM dd, yyyy hh:mm a", new Locale("es", "ES"));
// Print instant using the defined formatter
String formatted = outputFormatter.format(date);
System.out.println(formatted);
}
}
Output:
septiembre 10, 2020 08:00 p. m.
You can try something like this (it returns date in format: 10 de septiembre de 2020 20:00):
val format: DateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, // date format
DateFormat.SHORT, // time format
Locale("es", "ES") // Spanish Locale
)
val dateTime = "2020-09-10T20:00:00.000Z"
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale("es", "ES"))
val date: Date = simpleDateFormat.parse(dateTime)!! // without validation
println(format.format(date)) // it prints `10 de septiembre de 2020 20:00`

How get Date and Time from Timestamp to different TextView

I have timestamp in DataBase like this: 1472373943.
In my Adapter I would like set Date & Time to different TextView.
For Example:
How to separate the date and time? Thanks.
First, what is your input? In your question, it looks like 1472373943, So I don't know which is time, and date position in that string. For simple, let assume that you have a sample date and time as follows:
String date = "12/8/2012";
String time = "11:25 am";
So, to convert it. The simple way is
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
String date = "12/8/2012";
String time = "11:25 am";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
try {
Date dt = df.parse(date + " " + time);
Calendar ca = Calendar.getInstance();
ca.setTime(dt);
System.out.println(ca.getTimeInMillis());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
You can do that by using two different SimpleDateFormatters on the same Date object, one for the date and the other for the hours. "YYYY/MM/DD" and the other is "HH:mm a", just for an example.

How to use now Time in android studio

am c# developer and in c# you can use datetime.now to get this time as example but in android studio i do not know how to do that can you help me please .
not
I need just hour and minute thanks
You could use this example to get the current date:
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class GetCurrentDateTime {
public static void main(String[] args) {
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()));
}
}
Using either Date or Calendar gives the same result in this case. But if you need to set particular dates or do date arithmetic, use a Calendar. Calendars also handle localisation.
In Android you are programming in Java. Java Date provides the functionality you are looking for.
https://docs.oracle.com/javase/8/docs/api/java/util/Date.html

converting long string to date [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Convert UTC Epoch to local date
(16 answers)
How to convert String to long in Java?
(10 answers)
Closed 1 year ago.
I am getting date value from DB as a long value. I am converting this to string to use parse function. Given below is my code
Date date1 = new SimpleDateFormat("MM/dd/yyyy").parse(strDate1);
But the app is crashing when this code is executing.it will successfully execute if the
strDate1="12/30/2012".
But i am having this value as "12302012235"(pzudo value).
How can i do this?
edit:
i am saving date value to DB as INTEGER. from DB i am getting this value and converting to string.this is the actual strDate1 value
strDate1="1346524199000"
Try the following code segment:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(Long.parseLong(val));
Date d = (Date) c.getTime();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String time = format.format(d);//this variable time contains the time in the format of "day/month/year".
Try this,
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
Date dateD=new Date();
dateD.setTime(LongTime);
date=dateFormat.format(dateD);
Java 8, Convert milliseconds long to Date as String by given date format pattern. If you have a long milliseconds and want to convert them into date string at specifies time zone and pattern, then you can use it:-
dateInMs is a long value of DateTime.
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.format(Instant.ofEpochMilli(dateInMs).atZone(ZoneId.of("Europe/London")))
java.time
The java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API* .
Using modern date-time API:
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
long input = 12302012235L;
// Get Instant from input
Instant instant = Instant.ofEpochMilli(input);
System.out.println(instant);
// Convert Instant to ZonedDateTime by applying time-zone
// Change ZoneId as applicable e.g. ZoneId.of("Asia/Dubai")
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
System.out.println(zdt);
// Format ZonedDateTime as desired
// Check https://stackoverflow.com/a/65928023/10819573 to learn more about 'u'
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
String formatted = dtf.format(zdt);
System.out.println(formatted);
// If at all, you need java.util.Date
Date date = Date.from(instant);
}
}
Output:
1970-05-23T09:13:32.235Z
1970-05-23T10:13:32.235+01:00[Europe/London]
05/23/1970
Learn more about the modern date-time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
You can try following code:
private Date getGMTDate(long date) {
SimpleDateFormat dateFormatGmt = new SimpleDateFormat(
"yyyy-MMM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat(
"yyyy-MMM-dd HH:mm:ss");
Date temp = new Date(date);
try {
return dateFormatLocal.parse(dateFormatGmt.format(temp));
} catch (ParseException e) {
e.printStackTrace();
}
return temp;
}
I hope this will help you.
Try this
Date date1 = new SimpleDateFormat("MMddyyyySSS").parse(strDate1);
Hope it will works for 12302012235 , but i assume 235 is millisec.
i got the answer.actually i wanted to convert the string to date only for comparing the values.since i am getting the value as long i directly used the compareTo function to do this.avoided the conversion of long to string and string to date conversion.thank you all for support.

Android - get date time from SMS timestamp in miliseconds

is it possible to convert Sms table date value e.g. 1293457709636 (miliseconds) to meaningful Date time value.
just do
long ms = 1293457709636; // or whatever you have read from sms
Date dateFromSms = new Date(ms);
You can convert Sms table date in to meaningful Date time like this.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
long now = 1293457709636L;
calendar.setTimeInMillis(now);
Log.i("time", "time"+formatter.format(calendar.getTime()));

Categories

Resources