please tell me how to convert milliseconds to joda Date time??
formatter = DateTimeFormat.forPattern("dd/MM/yyyy'T'HH:mm:ss").withZone(DateTimeZone.forOffsetHoursMinutes(00, 00));
even tried
String millisecond="14235453511"
DateTime.parse(millisecond);
The answer given by #Adam S is almost okay. However, I would prefer to specify the timezone explicitly. Without specifying it you get the constructed DateTime-instance in the system timezone. But you want the zone "0000" (UTC)? Then look for this alternative constructor:
String milliseconds = "14235453511";
DateTime someDate = new DateTime(Long.valueOf(milliseconds), DateTimeZone.UTC);
System.out.println(someDate); // 1970-06-14T18:17:33.511Z
There's a constructor that takes milliseconds:
long milliseconds = 14235453511;
DateTime someDate = new DateTime(milliseconds);
You can use this
Calendar calendar = new GregorianCalendar();
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
formatter.setCalendar(calendar);
String timeZone = "GMT+2:00";
formatter.setTimeZone(TimeZone.getTimeZone(timeZone));
String time = formatter.format(calendar.getTime());
System.out.println(time);
I hope this will help you
Related
I would like to convert this time stamps to Long value.
2016-07-13T21:11:45+00:00
I still don't know what the format of above time stamp, like MM/dd/yyyy hh:mm:ss aa to use with the SimpleDateFormat.
Try this code to
String timeStr = "2016-11-01T09:45:00.000+02:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dateObj= sdf.parse(timeStr);
System.out.println(dateObj.getTime());
It would be yyyy-MM-dd\'T\'HH:mm:ss and you can convert it to long by:
public static long convertDateToMilliseconds(String fromFormat, String sourceDate) {
SimpleDateFormat sdfFrom = new SimpleDateFormat(fromFormat);
//SimpleDateFormat sdfTo = new SimpleDateFormat(toFormat);
Date date = sdfFrom.parse(sourceDate);
//String convertedDate = sdfTo.format(date);
return date.getTime();
}
Use this link to find in which formate to create Simple date formater then create date object with that date formater. Then you can get time in millis like below
long millisecond = beginupd.getTime();
SimpleDateFormat almost states this in its examples. For that you can use
"yyyy-MM-dd'T'HH:mm:ssXXX"
How can I get the current time and date according to phone device format?
my code is
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa",
Locale.ENGLISH);
String var = dateFormat.format(date));
It displays like 2PM instead of 14PM.
You can the device format data and time using
Date date = new Date();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
Log.d(TAG,dateFormat.format(date)+");
Use calendar object instead of Date because it is deprecated now and by using caledar object you can get hour in 12 hours formate as
int hour = calendarObject.get(Calendar.HOUR);
for 24 formate as:
int hour = calendarObject.get(Calendar.HOUR_OF_DAY);
for further detail you can visit this
Just change "hh:mm aa" to "HH:mm aa"
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.
I am trying to get a calendar object set to GMT, but the getTime() always returns the time in GMT+1 (my current time). I have tried:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("skeniver"));
They all apparently return GMT, because
cal.getTimeZone().getDisplayName()
returns "GMT+00:00"; but
cal.getTime().toString();
always displays the time in GMT+1.
Does anyone have any idea why this is happening?
You need to adjust for daylight savings. I'm not sure if this will help but it's code I use for adjusting any timezone to UTC in an app that's currently being used by a number of people around the world. I use Date instead of Calendar but it works...
Date dateTimeNow = new Date();
TimeZone tz = TimeZone.getDefault();
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(dateTimeNow) ? tz.getDSTSavings() : 0);
Date dateTimeNowUTC = new Date(dateTimeNow.getTime() - currentOffsetFromUTC);
If you want to in string then prefer the DateFormat or SimpleDateFormat for this
here is example
SimpleDateFormat sdf = new SimpleDateFormat(); // here you can also define your format of date for e.g. "dd/MM/yyyy z"
sdf.setTimeZone("GMT");
Calendar cal = Calendar.getInstance();
System.out.println(sdf.format(cal.getTime()));
Calendar.getTime() returns a Date object. In Java, a Date is just a holder to a long timestamp starting in the UNIX epoch.
To display a Date in a different TimeZone than the default, you can use a SimpleDateFormat.
I have seconds from epoch time and want to convert it to Day-Month-Year HH:MM
I have tried following but it gives me wrong value.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
Above code is not working properly am i doing anything wrong here.
For example if seconds = 1299671538
then it generates time string as Friday, December 12, 1969 which is wrong it should display Wednesday, March 09, 2011
For example if seconds = 1299671538 then it generates time string as Friday, December 12, 1969 which is wrong it should display Wednesday, March 09, 2011
You have integer overflow. Just use the following (notice "L" after 1000 constant):
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000L);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
or better use SimpleDateFormat class:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM d, yyyy HH:mm");
String dateString = formatter.format(new Date(seconds * 1000L));
this will give you the following date string for your original seconds input:
Wednesday, March 9, 2011 13:52
You need to use
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
instead of
Calendar cal = Calendar.getInstance();
because UTC time from seconds depends on Timezone.
You don't need a calendar in this case, you can simply use the constructor new Date(1000 * seconds)
Then use a SimpleDateFormat to create a String to display it.
For a full explanation on using SimpleDateFormat go here.
The answer to this question though is that you need to use long values instead of ints.
new Date(1299674566000l)
If you don't believe me, run this:
int secondsInt = 1299674566;
System.out.println(new Date(secondsInt *1000));
long secondsLong = 1299674566;
System.out.println(new Date(secondsLong *1000));
I can confirm that answer from #Idolon is working fine, simple snippet is below...
long created = 1300563523;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
String dateString = formatter.format(new Date(created * 1000L));
using SimpleDateFormat is better way, change the format as you need.
Won't it work wihtout Calendar, like below? Haven't run this piece of code, but guess it should work .
CharSequence theDate = DateFormat.format("Day-Month-Year HH:MM", objDate);