I'm learning Android a simple screen with user Information like name,Date of Birth and so on.
All are Text View and Edit Text.
In my pojo Date of Birth datatype is String.
When I enter date and try to get the date it is showing as some long number I this it is date and time in milliseconds.
Can any one help me to solve this.
If i understand your problem.. Try this...
Date d1 = new Date();
d1.setTime(time_in_milliseconds);
Calendar cal = Calendar.getInstance();
cal.setTime(d1);
And then you can get day,month, year..
int number_of_day = cal.get(Calendar.DAY_OF_MONTH)
Check this :
public Date getDateByFormat(String time, String format) {
if (time == null || "".equals(time)) return null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
return sdf.parse(time);
} catch (ParseException e) {
Log.d(Constants.TAG, "Could not parse date: " + time + " Due to: " + e.getMessage());
}
return null;
}
here format is your date string format. Like "yyyy-MM-dd HH:mm:ss"
Related
So Im trying to print the string "Eastern Daylight Time" instead of EDT . This should be dynamic and not hardcoded. Looking into DateFormatter class did not lead me to an answer that worked.
Here was an example that allows me to format but did not lead me to my specific answer.
I am getting the date back in the following format -
2013-06-08T00:00:00-04:00
Here are somethings that I have tried -
1)
String dateString = changeFormatDateStringWithDefaultTimeZone(paymentConfirmation.getTransactionDate(),
"yyyy-MM-dd'T'HH:mm:ssZ",
"M/d/yyyy hh:mm a zz");
public static String changeFormatDateStringWithDefaultTimeZone(String value, String ip_format, String op_format) {
if (value == null)
return null;
try {
SimpleDateFormat opSDF = new SimpleDateFormat(op_format, Locale.US);
opSDF.setTimeZone(TimeZone.getDefault());
SimpleDateFormat inSDF = new SimpleDateFormat(ip_format, Locale.US);
Date date = inSDF.parse(value);
return(opSDF.format(date));
} catch (Exception e) {
Log.e("Err", "Failed to convert time "+value);
e.printStackTrace();
}
return null;
}
2)
Date today = Calendar.getInstance().getTime();
String todayString = DateUtils.convertDateToStringWithTimeZone(today);
public static String convertDateToStringWithTimeZone(Date date){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = df.format(date);
dateString += " " + TimeZone.getDefault().getDisplayName(false, TimeZone.LONG);
return dateString;
}
These always print timezone as EDT and I want the string Eastern Daylight Time. Can anyone help me out with this?
Okay, based on your last edit of the question, the solution should be like this:
case 1)
The output pattern should be changed to "M/d/yyyy hh:mm a zzzz" (note the count of z-symbols to enforce the full zone name). Depending on the date and the underlying timezone, the formatter SimpleDateFormat will automatically determine if the daylight or the standard name is to be used.
case 2)
Use TimeZone.getDefault().getDisplayName(true, TimeZone.LONG) to enforce the long daylight name. If your default timezone is "America/New_York" then such an expression should print "Eastern Daylight Time". Note that the boolean parameter has been changed to true.
I am working on date format application, i have one T date format but i am not able to parse this format.
try {
String dtStart = "2016-07-20T11:51:31.744Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
if (dtStart != null) {
Date date = format.parse(dtStart);
Calendar myCal = new GregorianCalendar();
myCal.setTime(date);
String promoPendingDay = "" + myCal.get(Calendar.DAY_OF_MONTH);
int promoPendingMonth = myCal.get(Calendar.MONTH) + 1;
String promoPendingYear = "" + myCal.get(Calendar.YEAR);
mMessageStartDateLabel.setText(promoPendingDay + "/" + promoPendingMonth + "/" + promoPendingYear);
}
} catch (ParseException e) {
e.printStackTrace();
}
every time i am getting one exception java.text.ParseException: Unparseable date:
Please kindly go through my code and suggest me some solution.
Here a solution applying two changes. One change is to add the fractional seconds, another change is to set the UTC-timezone.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setTimeZone(TimeZone.getTimeZone("GMT")); // very important, input Z means UTC+00
I have a String of a date and time in my app eg:- 2014-10-30T11:30:00 I want to convert this String into my local time which should look something like this
2014-10-30T11:30:00+05:30. I cannot Manipulate the String as the server side conversion is done to do the addition and subtraction of the time. How do I add the +05:30 offset to my time?
How can I do it using the local time so that the server knows my locale?
Try this way,hope this will help you to solve your problem.
How to use given function :
convertDateStringFormat("2014-10-30T11:30:00","yyyy-MM-dd'T'hh:mm:ss","yyyy-MM-dd'T'hh:mm:ss")
Here you have pass three parameter as :
1.strDate : which is represent datetime string.
2.fromFormat : which is represent datetime format of your given datetime string.
3.toFormat : which is represent datetime format which you wan to convert your given datetime string.
public String convertDateStringFormat(String strDate, String fromFormat, String toFormat){
try{
SimpleDateFormat sdf = new SimpleDateFormat(fromFormat);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormat2 = new SimpleDateFormat(toFormat.trim());
return dateFormat2.format(sdf.parse(strDate));
}catch (Exception e) {
e.printStackTrace();
return "";
}
}
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
sdf.setTimeZone(tz);
long timestamp = cursor.getLong(columnIndex);
Log.d("Server time: ", timestamp);
String localTime = sdf.format(new Date(timestamp * 1000));
Log.d("Time: ", localTime);
I am debugging a android framework.
I pull out dropbox logs from device and it's created in /data/system/dropbox.
Log file name is printed like this format.
event_data#1362451303699
1362451303699 is timestamp and i want to change it like 05/03/2013 16:00 for legibility.
How can i convert this timestamp?
Is there any code needs to be changed?
Any help will be much appreciated.
use: Date date = new Date(timestamp);
Edit full code:
String wantedDate = "";
String log = "event_data#1362451303699";
int index = log.indexOf("#");
if(index != -1) {
index = index + 1; // skip # symbol
if(index < log.length()) { // avoid out of bounds
String logtime = log.substring(+1);
try {
long timestamp = Long.parseLong(logtime);
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = new Date(timestamp);
wantedDate = df.format(date);
} catch (NumberFormatException nfe) {
// not a number
}
}
}
if( ! "".equals(wantedDate) ) {
// everything OK
} else {
// error cannot retrieve date!
}
Related doc:
indexOf : http://developer.android.com/reference/java/lang/String.html#indexOf%28java.lang.String%29
SimpleDateFormat : http://developer.android.com/reference/java/text/SimpleDateFormat.html
you can use a SimepleDateFormat to parse it. For example:
long ts = 1362451303699;
Date date = new Date(ts);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
System.out.println(sdf.format(date));
With the SimpleDateFormat you can bring your Date in a more readable format.
It is a UNIX epoch timestamp, all you need to do is to convert the String representation of the number to long, then you can use it to create a Date object, which you can format with DateFormat. Something like this:
// Get this from the log
String timestamp = "1362451303699";
long epoch = Long.parseLong(timestamp);
Date date = new Date(epoch);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String formattedDate = format.format(date);
I have a problem that I have a timestamp value which is coming from JSON parsing, I am converting that timastamp in Date Time Format which is desired, all is working fine but month always return 1 in any case. means If timestamp contains aug month but it returns only 1 for any month. I don't know how to resolve this. Please suggest me the solution for the same.
Thanks in advance.
Code:
public String create_datestring(String timestring){
try{
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.parseLong(timestring));
timestring = String.valueOf(cal.get(Calendar.MONTH)+1) + "/" +String.valueOf(cal.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(cal.get(Calendar.YEAR));
}catch(NumberFormatException e){
timestring = "9/23/2011";--->timestring always returns 1 for month.
}
private String getDate(String timeStampStr){
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(Long.parseLong(timeStampStr)));
return sdf.format(netDate);
}
catch(Exception ex){
return dateInStr;
}
}
Try this one It may gives you satisfied output.
String DATE_FORMAT="hh:mm aa dd MMMM yyyy";
#SuppressLint("SimpleDateFormat")
private String getDate(long time) {
return new SimpleDateFormat(DATE_FORMAT).format(time * 1000);
}