This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to convert milliseconds to date format in android?
I have a Milliseconds String and want to convert it to a Date String.
Like this:
Milliseconds: 1345032624325E12 -> Date: Wed Aug 15 2012 14:10:24
How can i do this?
Hope this helps
how to convert milliseconds to date format in android?
private String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
Related
This question already has answers here:
How to convert Date to a particular format in android?
(7 answers)
Closed 3 years ago.
I have date in below format coming from api response and in pojo i take it as string and now date is taken as string and my concern is Date is deprecated in api 16.
My main question is how to convert this and assign some date format to it.
"date":1561040449000"
historyList.get(position).getTxnDate() = "1561040449000"
long dateToLong = Long.parseLong(historyList.get(position).getTxnDate());
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
String dateString = sdf.format(new Date(dateToLong));
Pass your long typed "date" timestamp to this method and it will return date in specified format:
public static String getDateFromTimeStamp(long date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(new Date(date));
}
This question already has answers here:
How to convert Gregorian string to Gregorian Calendar?
(3 answers)
Closed 4 years ago.
I converted Gregorian Calendar to String by using toString() method to upload my DataBase. ( Because My Database doesn't apply Calendar type )
Here Example.
String Time =
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=81,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
How do I parse it to Calendar type?
Much appreciate if you guys could help.
Use SimpleDateFormat like this.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,23); (etc)
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = simpleDateFormat.format(calendar.getTime());
Well, you can save GregorianCalendar's millis in the database which will be of a long type. Here is how you do it:
long millis = GregorianCalendar.getInstance().getTimeInMillis();
and when you want the GregorianCalendar instance in your app just get the millis from the database and add those millis to GregorianCalendar instance like this :
GregorianCalendar gregorianCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
gregorianCalendar.setTimeInMillis(millis);
Don't save the string representation of Calendar. Save the timestamp as Long.
Here an example:
Calendar cal1 = GregorianCalendar.getInstance();
long timeStamp = cal1.getTimeInMillis();
Calendar cal2 = GregorianCalendar.getInstance();
cal2.setTimeInMillis(timeStamp);
This question already has answers here:
Convert JSON date format
(5 answers)
Closed 5 years ago.
How do I parse this date format:
"/Date(1514728800000+0300)/"
I don't know what the meaning of this number is, or that of the + sign.
Partial answer: the number is the epoch time.
This is the amount of seconds since Jan 1, 1970, UTC.
You can pass this value to the constructor of java.util.Date, which will get you a Date object with the right value.
The +0300 is unclear, perhaps a reference to a different timezone.
Im not sure about the +0300, but you can convert a epoch time to Date with the following function:
Date date = new Date(Long.parseLong(myDateToParse.replaceAll("[^\\d-]", "")));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.DEFAULT);
Here is the code that parses this assuming you have unixtimestamp_zoneoffset
String inputStr = "1514728800000+0300";
String[] splitStr = inputStr.split("[+-]");
String offsetSign = inputStr.indexOf("+")>0 ? "+" : "-";
ZonedDateTime captureTime = Instant.ofEpochMilli(Long.valueOf(splitStr[0])).atZone(ZoneOffset.of(offsetSign+splitStr[1]));
The value returned is 2017-12-31T17:00+03:00
This question already has an answer here:
How to get day, month, year and hour, minutes, second from DateTime format?
(1 answer)
Closed 8 years ago.
I have a field in mysql database of type DateTime.The value in this field is sent as a string to my android app( say in the form "2014-11-21 06:00:00") .I need to obtain year,month,day,hours and minutes value from the string and set it to a calendar instance.Please help me.
String s = "2014-11-21 06:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse(s));
System.out.println(calendar.getTime());
To convert your String in a Date object you can use a SimpleDateFormat:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
Date date = simpleDateFormat.parse("2014-11-21 06:00:00");
Then you can use a Calendar with the Date object you got before
Calendar c = Calendar.geInstance();
c.setTime(date);
and use c.get(Calendar.SECOND) to get the seconds, for instance
This question already has answers here:
Display the current time and date in an Android application
(23 answers)
Closed 9 years ago.
I need to format a date and time as Nov 15 2012 18:55 GMT.
Time should be in GMT and 24hr format.I have tried using the following code but it gives time in 12hr format.Is there any way?
DateFormat currentDateTimeString = DateFormat.getDateTimeInstance();
currentDateTimeString.setTimeZone(TimeZone.getTimeZone("gmt"));
gmtTime = currentDateTimeString.format(new Date());
You can use DateFormats to convert Dates to Strings in any timezone:
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());
OR
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
String localTime = date.format(currentLocalTime);
System.out.println(localTime);