Well Im using Joda-time
I want to convert the Georgian to Hijri date but it return the wrong date from georgian to hijri.
Im doing like this
TimeZone timeZone = TimeZone.getTimeZone("UTC+05:00"); // Pakistan Time Zone
DateTimeZone datetimeZone = DateTimeZone.forTimeZone(timeZone);
Chronology iso = ISOChronology.getInstance(datetimeZone);
Chronology hijri = IslamicChronology.getInstance(datetimeZone);
LocalDate todayGeorgian = new LocalDate(2014,04,13), iso); //Today's Date
LocalDate todayHijri = new LocalDate(todayGeorgian.toDateTimeAtStartOfDay()), hijri);
todayHijri.toString(); // This must return **1435-06-13** but returns 1435-06-12
Problem appears when you do redundant call of LocalDate#toDateTimeAtStartOfDay() method.
LocalDate object is timezone independent, but when you convert it to DateTime (by #toDateTimeAtStartOfDay() method ), it becomes zone dependent.
After it you convert DateTime to LocalDate again.
This conversions cause the issue.
Please, don't use LocalDate#toDateTimeAtStartOfDay() here:
Chronology iso = ISOChronology.getInstance(datetimeZone);
Chronology hijri = IslamicChronology.getInstance(datetimeZone);
LocalDate todayGeorgian = new LocalDate(2014, 04, 13, iso); //Today's Date
LocalDate todayHijri = new LocalDate(todayGeorgian, hijri);
EDIT
Also "UTC+05:00" is not valid zone for TimeZone. Use:
DateTimeZone datetimeZone = DateTimeZone.forID("Etc/GMT+5");
Related
I want to get current time specifically in English to save it in Database,
to get Current time i use function
private String get_current_Time() {
String CURRENT_TIME_FORMAT = "yyyy_MM_dd_HH_hh_mm_ss_a_MMMM_MMM_EEEE_EE";
return (String) DateFormat.format(CURRENT_TIME_FORMAT, Calendar.getInstance().getTime());
}
but when i set Locale to different language it gives me current time in that language.
for example if i set
conf.setLocale(new Locale("mr"));
it gives me date in marathi. I specifically want it in English. How to do it.
And Also how to change the language of Date once it is saved.I mean if I have saved date in English and while display i want that date to be shown in some other language, how to do it.?
As ADM suggested. new function that worked
public String get_current_Time() {
String CURRENT_TIME_FORMAT = "yyyy_MM_dd_HH_hh_mm_ss_a_MMMM_MMM_EEEE_EE";
SimpleDateFormat dateFormat = new SimpleDateFormat(CURRENT_TIME_FORMAT, Locale.ENGLISH);
return dateFormat.format(Calendar.getInstance().getTime());
}
So now it always returns Date in English
This should fix your issue! Try any of these
here is a simple tutorial
For java.util.Date, just create a new Date()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
log.d(dateFormat.format(date)); //2016/11/16 12:08:43
For java.util.Calendar, uses Calendar.getInstance()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
log.d(dateFormat.format(cal)); //2016/11/16 12:08:43
For java.time.LocalDateTime, uses LocalDateTime.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
log.d(dtf.format(now)); //2016/11/16 12:08:43
For java.time.LocalDate, uses LocalDate.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
log.d(dtf.format(localDate)); //2016/11/16
I am trying to convert ISO 8601 time into something human readable and in the local timezone of the Android device.
String date = "2016-09-24T06:24:01Z";
LocalDate test = LocalDate.parse(date, ISO_INSTANT);
But it returns:
method threw 'org.threeten.bp.format.DateTimeParseException' exception
From reading http://www.threeten.org/threetenbp/apidocs/org/threeten/bp/format/DateTimeFormatter.html#ISO_INSTANT it seems like what I'm doing should be possible.
What am I doing wrong?
Edit
Expanded exception error:
Unable to obtain LocalDate from TemporalAccessor: DateTimeBuilder[fields={MilliOfSecond=0, NanoOfSecond=0, InstantSeconds=1474698241, MicroOfSecond=0}, ISO, null, null, null], type org.threeten.bp.format.DateTimeBuilder
Edit 2
The solution is in the answer below. For anyone that stumbles across this, if you want to specify a custom output format you can use:
String format = "MMMM dd, yyyy \'at\' HH:mm a";
String dateString = DateTimeFormatter.ofPattern(format).withZone(ZoneId.systemDefault()).format(instant);
#alex answer is correct. Here is a working example.
Instant represents a point in time. To convert to any other local types you will need timezone.
String date = "2016-09-24T06:24:01Z";
This date string is parsed using the DateTimeFormatter#ISO_INSTANT internally.
Instant instant = Instant.parse(date);
From here you can convert to other local types just using timezone ( defaulting to system time zone )
LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
LocalTime localTime = instant.atZone(ZoneId.systemDefault()).toLocalTime();
Alternatively, you can use static method to get to local date time and then to local date and time.
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime();
You need to use Instant.parse().
This will give you an Instant that you can combine with a time zone to create a LocalDate.
In Kotlin:
Converts to LocalDateTime directly based on your local time zone::
val instant: Instant = Instant.parse("2020-04-21T02:22:04Z")
val localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime()
Converts to Date and time separately based on your local time zone:
val localDate: LocalDate = instant.atZone(ZoneId.systemDefault()).toLocalDate()
val localTime: LocalTime = instant.atZone(ZoneId.systemDefault()).toLocalTime()
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
I convert json date to human readable date but it shows less one then actual date. I
used this code to convert it:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Long timeInMillis = Long.valueOf(AttendanceModelList.get(position).getEmpdate());
calendar.setTimeInMillis(timeInMillis);
Date date=new Date(timeInMillis);
viewHolder.textemployeedate.setText(df.format(date));
Please help
You say as summary:
Your calendar date is one day less than expected when you try to
interprete a global timestamp of type java.util.Date as calendar
date.
This phenomenon can happen due to timezone effects or midnight change. Before viewing the technical solution, you have to ask yourself:
What is your default (system) timezone using TimeZone.getDefault()?
Do you run your code on a server which has not the expected timezone?
In which timezone do you wish to view the calendar date? (the timezone associated with your expected "actual" date)
How to specify the timezone?
java.util.Date d = ...; // from your JSON-timeInMillis?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String tz = "Asia/Kolkata"; // or any other valid tz id
sdf.setTimeZone(TimeZone.getTimeZone(tz));
System.out.println(sdf.format(d));
I would like to convert a Date from my current TimeZone to UTC.
The results are not understandable for me.
Code:
public static String convertToUTC(String dateStr) throws ParseException
{
Log.i("myDateFunctions", "the input param is:"+dateStr);
String uTCDateStr;
Date pickedDate = stringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
Log.i("myDateFunctions", "the input param after it is converted to Date:"+pickedDate);
TimeZone tz = TimeZone.getDefault();
Date now = new Date();
Log.i("myDateFunctions:", "my current Timezone:"+tz.getDisplayName()+" +"+(tz.getOffset(now.getTime()) / 3600000));
// Convert to UTC
SimpleDateFormat converter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
converter.setTimeZone(TimeZone.getTimeZone("UTC"));
uTCDateStr = converter.format(pickedDate);
Log.i("myDateFunctions", "the output, after i converted to UTC timezone:"+uTCDateStr);
return uTCDateStr;
}
And LogCat results are:
03-29 20:31:46.804: I/myDateFunctions(18413): the input param is:2014-04-29 20:00:00
03-29 20:31:47.005: I/myDateFunctions(18413): the input param after it is converted to Date:Tue Apr 29 20:00:00 CEST 2014
03-29 20:31:47.005: I/myDateFunctions:(18413): my current Timezone:Central European Time +1
03-29 20:31:47.005: I/myDateFunctions(18413): the output, after i converted to UTC timezone:2014-04-29 18:00:00
As you can see:
My TimeZone is CET (GMT+1)
Then why if my input is 20:00 i get back 18:00 instead of 19:00 ?
The problem is daylight savings time. UTC doesn't have it, if yours does it will increase the difference by 1 hour during part of the year.
The answer by Game Sechan appears to be correct.
I just want to show how much easier this work is when using Joda-Time or java.time rather than the notoriously troublesome java.util.Date and .Calendar classes.
Joda-Time
In Joda-Time 2.4.
String inputRaw = "2014-04-29 20:00:00";
String input = inputRaw.replace( " ", "T" );
DateTimeZone timeZoneIntendedByString = DateTimeZone.forID( "America/Montreal" ); // Or DateTimeZone.getDefault();
DateTime dateTime = new DateTime( input, timeZoneIntendedByString );
DateTime dateTimeUtc = dateTime.withZone( DateTimeZone.UTC ); // Adjust time zones, but still same moment in history of the Universe.