How can i convert dropbox log timestamp? - android

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);

Related

how can i get the date full time of specific string format

I have an API contains string date with this format "/Date(1527677209864)/", how can I get the date and time to be used in Android app
You can use Date with the epoch number as a parameter in the constructor.
First you have to strip /Date( and )/ from the string, this you can do with regex.
Pattern pattern = Pattern.compile("\\D+([0-9]+)\\D+");
Matcher matcher = pattern.matcher("/Date(1527677209864)/");
if (matcher.matches()) {
long timestamp = Long.parseLong(matcher.group(1));
Date actualDate = new Date(timestamp);
}
I'm assuming that 1527677209864 value is a timestamp, right?
Try this function:
public static String getDateAndTime(#NotNull Context context, long timestamp) {
Date date = new Date(timestamp * 1000);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
DateFormat df = new SimpleDateFormat(getTimeFormat(context), Locale.getDefault());
return df.format(date);
}
Let me know if this is what you were looking for.
Remove the prefix /Date( and suffix )/. Use the result to initialize a java.util.Date.
String dt = "/Date(1527677209864)/";
dt = dt.substring(6, dt.indexOf(")/"));
long timestamp = Long.parseLong(dt);
Date date = new Date(timestamp);
Use SimpleDateFormat to format this date to your required format.
We have to filter the timeInMillis value from the string and convert it to long so that we can use or set it in calendar and get the date object.
Date convertToDate(String input) {
// input = "/Date(1527677209864)/";
String timeString = input.substring(6, input.length() - 2);
Long time = Long.parseLong(timeString);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return calendar.getTime();
}
Then we can format the date object to string according to our desire patter or format.
String convertDateToString(Date date, String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
}
Try this
String jsonDate = "/Date(1527677209864)/";
jsonDate = jsonDate.substring(6, 13);
int unix_timestamp = Integer.parseInt(jsonDate);
Date date = new Date(unix_timestamp);

Value of EditText (InputType: Date) to integer

i want to get the value from EditText(InputType: Date) to integer.
Example: 10.01.2017 ==> 20170110
DateFormat df = new SimpleDateFormat("yyyyMMdd");
int inputDate = Integer.valueOf(df.format(etDate.getText().toString()));
How can i fix my problem?
thx and regards
You can parse date. then form the Date object you can have millies
DateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
Date date=df.parse(textView.getText().toString());
long millies=date.getTime();// Here is milli seconds
} catch (ParseException e) {
e.printStackTrace();
}
Edit
To convert one format to another . you can use .
DateFormat df = new SimpleDateFormat("yyyyMMdd");
DateFormat inputFormate = new SimpleDateFormat("dd.MM.yyyy");
try {
Date date=inputFormate.parse(textView.getText().toString());
String formated_date=df.format(date);
// this will be formated_date
} catch (ParseException e) {
e.printStackTrace();
}
Keep that in mind that use can input anything . So you need first check, in which format user entered date it can be (dd/MM/yyyy) or (MM/dd/yyyy).
To get extra data from date or millies use Calender.
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(millies);
int day=calendar.get(calendar.DAY_OF_MONTH);
String text = editText.getText().toString();
String[] array = text.split("\\.");
StringBuilder stringBuilder = new StringBuilder();
for (int i = array.length - 1; i >= 0; i--) {
stringBuilder.append(array[i]);
}
int date = Integer.parseInt(stringBuilder.toString());
I have this stupid way to help you.If you want want to accept this solution, maybe use Regular to solve it is better.

How to convert "year-month-day" date format(eg: 2015-05-12-which is a value retrieved from server) to "day-month-year" format in android

How to convert "year-month-day" date format(eg: 2015-05-12-which is a value retrieved from server) to "day-month-year" format in android .
The actual value I get from server is in yr-mnth-day format. But I need to display it as day-mnth-yr in my app.How can I achieve this?
First you'll need to parse the String into a Date. Then you format the date according to your needs, the example below is an edited version of BalusC's answer from this topic: Change date format in a Java string
String oldstring = "2015-05-12";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
//Use SimpleDateFormat#format() to format a Date into a String in a certain pattern.
String newstring = new SimpleDateFormat("dd-MM-yyyy").format(date);
System.out.println(newstring); // 12-05-2015
If you say this, i suppose you mean in a string, so you can convert string to Date with
String dateServer = "2015-05-12";
//Identify the format which has been used from server
DateFormat format = new SimpleDateFormat("yyyy, MM, dd", Locale.ENGLISH);
//Convert string to Date using this format
Date date = format.parse(string);
//Create the format you need to print
DateFormat wellFormatted = new SimpleDateFormat("dd/MM/yyyy");
//now convert the date into a string formatted as you like
String wellPrintedDate = wellFormatted.format(date);
that's all, "wellPrintedDate" is the string in format day/month/yeah. here you can find the list of code used for formatting :)
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date past = format.parse("2015-05-12");
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("dt=" + format1.format(past));
} catch (ParseException e) {
e.printStackTrace();
}
public static String formattedDateFromString(String inputFormat, String outputFormat, String inputDate){
if(inputFormat.equals("")){ // if inputFormat = "", set a default input format.
inputFormat = "yyyy-MM-dd";
}
if(outputFormat.equals("")){
outputFormat = "dd-mm-yyyy"; // if inputFormat = "", set a default output format.
}
Date parsed = null;
String outputDate = "";
SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());
// You can set a different Locale, This example set a locale of Country Mexico.
//SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("es", "MX"));
//SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("es", "MX"));
try {
parsed = df_input.parse(inputDate);
outputDate = df_output.format(parsed);
} catch (Exception e) {
Log.e("formattedDateFromString", "Exception in formateDateFromstring(): " + e.getMessage());
}
return outputDate;
}
call like this
Log.e("",""+formattedDateFromString("yyyy-MM-dd","dd-MM-yyyy","2015-05-12"));
output: 12-05-2015

How to get local time Android

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);

Date format to match the Date format in Phone Settings

I have a string which returns the date and time as 2012-11-08 12:45:30 . I need to get the date and time in separate strings and then the date has to be shown in the format which is there in the Phone's Date and Time settings.
Here is the code which I have tried so far:
date value from db is 2012-11-08
I am getting the date format in phone's settings as
String datefrmt = Settings.System.getString(
context.getContentResolver(), Settings.System.DATE_FORMAT);
the code to apply this format to the obtained date from db is:
SimpleDateFormat sdf = new SimpleDateFormat(datefrmt);
java.util.Date date_1 = sdf.parse("2012-11-08");
String s = sdf.format(date_1);
I am getting the month and day properly but the year its returning something randomly and thats not a correct value. Can anyone please guide me where am I going wrong. Thanks
If you have 2012-11-08 12:45:30 string and you want to parse it to Date object and change its format to another (system format for example or 2012-11-08 format):
try {
String yourString="2012-11-08 12:45:30";
//first we parse input date string and create Date object
String inputDateString ="yyyy-dd-MM hh:mm:ss";
SimpleDateFormat inputDateFormat = new SimpleDateFormat(inputDateString);
java.util.Date date = inputDateFormat.parse(yourString);
//log
Log.e(getClass().getName(), inputDateFormat.format(date));
//create system date format
String dateformt = Settings.System.getString( this.getContentResolver(), Settings.System.DATE_FORMAT);
//if its for some reason==null let it be "yyyy-dd-MM"
if(dateformt==null){
dateformt="yyyy-dd-MM";
}
SimpleDateFormat systemDateFormat = new SimpleDateFormat(dateformt);
String outputString = systemDateFormat.format(date);
//log
Log.e(getClass().getName(), outputString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
you can use the simpleDateFormat as:
SimpleDateFormat simpDate = new SimpleDateFormat(datefrmt,
Locale.ENGLISH);
String s = simpDate.format(new Date());
for reference, you can refer to developer site:
for more information refer this link
Since your DB date is in yyyy-MM-dd, don't use the dynamic format to get the date, otherwise it will crash(throw parse exception), instead use the static format as below to get the date. You may use the dynamic format to convert the string as below:
//use static format to convert into date from static formatted data in db
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date_1 = sdf1.parse("2012-11-08");//got the date
//use the dynamic format to convert into string
SimpleDateFormat sdf2 = new SimpleDateFormat(datefrmt);
String s = sdf2.format(date_1);
Since final String format = Settings.System.getString(getActivity().getContentResolver(), Settings.System.DATE_FORMAT);//may be NULL
So I customized my function as below for general purposes
#SuppressLint("SimpleDateFormat")
public String getCurrentShortDateFormat() {
final String[] SFs = new String [] {
"MM/dd/yyyy",
"dd/MM/yyyy",
"yyyy/MM/dd"
};
final Date date = new Date(24638400000L);//24638400000=Oct/13/1970; 0==Jan/1/1970
java.text.DateFormat shortDateFormat = android.text.format.DateFormat.getDateFormat(getActivity().getApplicationContext());
String current = shortDateFormat.format(date);
SimpleDateFormat which = new SimpleDateFormat();
for (int i = 0; i < SFs.length; i++) {
which.applyPattern(SFs[i]);
if (which.format(date).compareTo(current)==0)
return SFs[i];
}
return null;
}
#Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "----- onResume.CurrentShortDateFormat-----" + getCurrentShortDateFormat());
}

Categories

Resources