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"
Related
I want today's date in my application to store it in a database as a string but when i used simpledatefromat and calender.getInstance(), it's not giving me proper date. The code is as below.
I tried simpledateformat's another constructor which takes locale as parameter but still not showing proper date
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
String dateF = dateFormat.format(date);
expected date should be 21-04-2019
actual result 21-26-2019
To get the month like the way that you want, you need to change "dd-mm-yyyy" to "dd-MM-yyyy"
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
String dateF = dateFormat.format(date);
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
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 have to get count of days which are past to the current day.I have list of days in arraylist.I got the list and I dont know how to compare?Can anyone help me?
This is the code I tried,
private void weeklylogeval(){
int i;
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormatter.setLenient(false);
Date today = new Date();
String s = dateFormatter.format(today);
System.out.println("current date & time new:::"+s);
for(i=0;i<datetime.size();i++){
String daytime=datetime.get(i);
if(today.before(daytime))
}
}
Pls some one help me!
Try this code for date difference manipulation.
String fd=from_date;//date get from mysql database as string.
String td=to_date;//Today's date as string.
if(!fd.equalsIgnoreCase("") && !td.equalsIgnoreCase("")){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter;
Date frmdt=new Date(fd);
formatter = new SimpleDateFormat("dd/MM/yyyy");
String s1 = formatter.format(frmdt);
Date todt=new Date(td);
String s2 = formatter.format(todt);
Date frmdate = sdf.parse(s1);
Date todate = sdf.parse(s2);
if(frmdate.compareTo(todate)<=0) {
//do your stuff
} else {
// do your stuff
}
}
http://developer.android.com/reference/java/util/Calendar.html
This should be allot easier to use for your purpose
Edit:
Methods you can use:
boolean after(Object calendar)
Returns whether the Date represented by this Calendar instance is after the Date represented by the parameter.
boolean before(Object calendar)
Returns whether the Date represented by this Calendar instance is before the Date represented by the parameter.
Maybe you can construct a Date form the String you get from DB, and then use today.before(daytime) to compare them.
Date daytime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(datetime.get(i));
I am trying to get long value of a string (date & time string), but it is not working. What I am trying to do is:
Choose date form datepicker and store it in a String
Choose time from timepicker and store it in a String
Then I concatenate these two strings and get long value from that string.
I have tried a few Date formatters but I am unable to get this done. The format of my string is dd-MM-yyy h:mm a. Please help me out of this. Provide any utility available for this.
Try this:-
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyy h:mm a");
Date myDate = new Date(); // Default Value.
try {
myDate = sdf.parse(dateString);
} catch (ParseException e) {
// Do Something on Error.
}
Long dateTimeinLong = myDate.getTime();
where dateString is your concatenated String of date and time.
Forget about strings, and go directly with the values:
DatePicker dp = (DatePicker) findViewById...
TimePicker tp = (TimePicker) findViewById...
Date timeStamp = new Date( dp.getYear(), dp.getMonth(), dp.getDay(), tp.getHour(), tp.getMinute(), 0 );
long longTime = timeStamp.getTime();