I want to convert my date "2013-03-04T23:00:00" to "yyyy-MM-dd'T'HH:mm:ss.SSSZ" but I keep getting "2013-03-04'T'23:00:00.000+0000". Any help?
item.getEnd() is a XMLGregorianCalendar by the way.
Calendar calendar = item.getEnd().toGregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = calendar.getTime();
DateTime iso8601 = new DateTime(calendar);
iso8601.toString("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
You should just be able to use the ISODateTimeFormat class that is built in to JodaTime. The example in the documentation is:
DateTime dt = new DateTime();
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
String str = fmt.print(dt);
Related
Using this code
String twelveHourTime="06:00 PM";
public static DateTime convert12HourTimeTo24HourTime(String twelveHourTime) {
DateTimeFormatter dateTimeFormatter =
DateTimeFormat.forPattern(AppConstants.TWELVE_HOUR_TIME_FORMAT);
DateTime dateTime = dateTimeFormatter.parseDateTime(twelveHourTime);
return new DateTime().withHourOfDay(dateTime.getHourOfDay())
.withMinuteOfHour(dateTime.getMinuteOfHour());
}
I am getting this date time:
String datetime=2017-09-15T18:00:23.153+05:30
Now I want to convert it to the US time zone.
Please suggest me how to do this.
You can use SimpleDateFormat for conversion
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH24:MI");
Date date = df.parse(datetime);
Use localDateTime:
DateTime dt = new LocalDateTime(timestamp.getTime()).toDateTime(DateTimeZone.UTC);
you can use it by using TimeZone and SimpleDateFormat :-
TimeZone time = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(time);
final Date startDate = cal.getTime();
SimpleDateFormat sdfAmerica = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
sdfAmerica.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String sDateInAmerica = sdfAmerica.format(startDate);
edDate.setText(sDateInAmerica);
I have hour and minute in edittext.(say for example 10:50)
how to get today's DateTime(like yyyy-MM-dd HH:mm:ss) based on above edit text value 10:50
UPDATE:
this worked for me:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, selectedHour);
calendar.set(Calendar.MINUTE, selectedMinute);
thank you all
EDIT: take a look at this other stackoverflow post about why one should prefer using the Java 8 java.time classes over Calendar or Date
In your instance, in order to parse an input in the form "HH:mm", you can create a DateTimeFormatter and use the static method LocalTime.parse(inputString, dateTimeFormatter) to get a LocalTime object
For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime time = LocalTime.parse(timeInputString, formatter);
Take a look at the Java documentation for DateTimeFormatter for more information on the patterns for years, days, or whatever.
If you're wanting to get both the date and time from a string such as "10:50", you won't be able to using the LocalDateTime.parse method because it can't obtain the date from an "HH:mm" string. So the way around this is to create the time off of LocalTime.parse as shown above, and then get the current date using the static method LocalDate.now(); And then combine those two results into a LocalDateTime object using it's static factory method LocalDateTime.of(date, time)
For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime time = LocalTime.parse(timeInputString, formatter);
LocalDate date = LocalDate.now();
LocalDateTime dateTime = LocalDateTime.of(date, time);
Calendar cal = new Calendar.getInstance();// today's date
cal.set(Calendar.HOUR, 10);
cal.set(Calendar.MINUTE, 50);
cal.set(Calendar.SECOND, 0);
System.out.println(cal.getTime()); //see the result
now , it's up to you to put them directly in cal.set with variables (not manually writing 10 ,50 , it's up to your creativity )
And this answer is with Calendar because you tagged Calendar in your question , you can also use something else than Calender
you just need use this function.
// hourMintText = "hh:mm"
private String getTodayAsFormat(String hourMintText){
String[] hourMinAsArray = hourMintText.split(":");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourMinAsArray[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(hourMinAsArray[1]));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm", Locale.ENGLISH);
return simpleDateFormat.format(calendar.getTimeInMillis());
}
Try this:
String dateTime = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss aa", Locale.getDefault())
.format(new Date());
I have a below program to compare two dates.
I get timestamps that are date1 and currentTimestamp, here i need to compare only dates not the time values.But below program always returns -1.(value)
timestamp date1 = "2017-01-20 14:51:30.091" // i get this from service call in this format
SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_TIMESTAMP_FORMAT);
Calendar calendar = Calendar.getInstance();
String formattedDate = dateFormat.format(calendar.getTime());
java.util.Date currentDate = dateFormat.parse(formattedDate);
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(currentDate.getTime());
int value = DateTimeComparator.getDateOnlyInstance().compare(date1 , currentTimestamp );
How to compare only dates regardless of time. Please help me on this.
UPDATED:
i changed to below code
timestamp date1 = "2017-01-20 14:51:30.091" // i get this from service call in this format
LocalDate localDate = new LocalDate();
int value = DateTimeComparator.getDateOnlyInstance().compare(date1 , localDate );
this gives me error saying "java.lang.IllegalArgumentException: No instant converter found for type: org.joda.time.LocalDate"
This is NOT Joda solution...
Use: Apache library 'commons-lang3-x.x.jar' (DateUtils)
#Test
public void testCompare() {
Date current = new Date();
Date previous = DateUtils.addHours(DateUtils.addDays(new Date(), -1),5);
assertEquals(1,DateUtils.truncatedCompareTo(current,previous,Calendar.DATE));
assertEquals(0,DateUtils.truncatedCompareTo(current,current,Calendar.DATE));
assertEquals(-1,DateUtils.truncatedCompareTo(previous,current,Calendar.DATE));
}
You can Try this code with JodaTime :
DateTime date1 = DateTime.parse("2017-01-20 14:51:30.091", DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS"));
DateTime now = new DateTime();
int value = DateTimeComparator.getDateOnlyInstance().compare(date1 , now);
To compare DateTime in joda without time you have 2 options:
convert DateTime to LocalDate .
Use DateTimeComparator.getDateOnlyInstance
For example:
#Test
public void compareJodaTime() {
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
String today = "2017-01-20 14:51:30.091";
String tomorrow = "2017-01-21 14:51:30.091";
DateTime now = DateTime.now();
Assert.assertThat(now.toLocalDate().compareTo(DateTime.parse(today, dateTimeFormatter).toLocalDate()), Matchers.is(0));
Assert.assertThat(now.toLocalDate().compareTo(DateTime.parse(tomorrow, dateTimeFormatter).toLocalDate()), Matchers.is(-1));
Assert.assertThat(now.toLocalDate().isEqual(DateTime.parse(today, dateTimeFormatter).toLocalDate()), Matchers.is(true));
Assert.assertThat(now.toLocalDate().isBefore(DateTime.parse(tomorrow, dateTimeFormatter).toLocalDate()), Matchers.is(true));
Assert.assertThat(DateTimeComparator.getDateOnlyInstance().compare(now, DateTime.parse(today, dateTimeFormatter)), Matchers.is(0));
Assert.assertThat(DateTimeComparator.getDateOnlyInstance().compare(now, DateTime.parse(tomorrow, dateTimeFormatter)), Matchers.is(-1));
}
hi i am using Joda time to convert my string dates to DateTime objects.
I currently have the following string:
2014-02-16T00:17:20.000Z
how do i convert this to a DateTime object?
I have tried:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZZ");
DateTime dt = formatter.parseDateTime("2014-02-16T00:17:20.000Z");
But i am getting the following error:
java.lang.IllegalArgumentException: Invalid format: "2014-02-16T00:17:20.000Z" is malformed at ".000Z"
Any help is greatly appreciated
For future visitors, simpler solution:
String date = "2014-02-16T00:17:20.000Z";
DateTime dateTime = new DateTime(date);
This format happens to be the ISO date time format, that DateTime uses by default. You just need
DateTime d = DateTime.parse(s);
or
DateTime d = DateTime.parse(s, ISODateTimeFormat.dateTimeParser());
Might be issue is you guys using Z(zone) in caps
i have tested below code works well
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.ENGLISH);
Date date =formatter.parse("2016-09-06T08:35:02.530GMT");
DateTime d = new DateTime(date.getTime());
I am trying to use the following code, but I am getting an error Invalid format: "12/11/2013":
String dFrom = ps.utils.gv(request, "dFrom");
String dTo = ps.utils.gv(request, "dTo");
DateTime dateFrom = new DateTime(dFrom);
DateTime dateTo = new DateTime(dTo);
int weeks = Weeks.weeksBetween(dateFrom, dateTo).getWeeks();
Could somebody please provide an example of how to format the date variable dFrom which is typically a UK formatted date such as 12/11/2013 to an ISO Date such as 2013-11-12 which I believe Joda supports.
Any help would be much appreciated :-)
If you want convert format 12/11/2013 to 2013-11-12, you can use
DateTimeFormatter dtf = DateTimeFormat.forPatter("dd/MM/yyyy"); // or MM/dd/yyyy ?
String isoDate = ISODateTimeFormat.date().print(dtf.parseDateTime("12/11/2013"));
For ISO format 2013-11-12 you can use standart date formatter:
ISODateTimeFormat::date()
DateTime date = ISODateTimeFormat.date().parseDateTime("2013-11-12");
String dateAsString = ISODateTimeFormat.date().print(date);
For format 12/11/2013 you should create your own formatter
DateTimeFormatter dtf = DateTimeFormat.forPatter("dd/MM/yyyy"); // or MM/dd/yyyy ?
DateTime date = dtf.parseDateTime("12/11/2013");
String dateAsString = dtf.print(date);