How to convert date string "/Date( -62135575200000-0600)/" - android

One of the attributes in JSON have date value ("/Date( -62135575200000-0600)/"), I'm not familiar with the type. How to convert this string to SimpleDateFormat
JSON attribute:
"toDate": "/Date( -62135575200000-0600)/"

This should work:
String date = "-62135575200000-0600"; // strip down the numerical value
Date date1 = new Date(date);
DateFormat fmt = new SimpleDateFormat("yyyy mm dd");
System.out.println(fmt.format(date1));
Negative date is taken as milliseconds before 1970 jan 1.
Source

Related

Simpledateformat is not giving proper date

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

How can I convert String Date Format in Android?

I have a string that contains a date like so:
String startTime = "2014-10-11T17:00:41+0000"
I am trying to reformat that string so that it reads like so instead:
Oct 11, 2014 5:00 PM
Since Date objects do not keep time zone information, you need to specifically set the time zone offset of original date to the target formatter. Here is the complete code for transforming from one format to another while maintaining the time zone offset (+0000 in your case). More information on TimeZone class here and on how to build a proper date and time pattern string for your requirement here.
try {
DateFormat originalFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat(
"MMM dd, yyyy K:mm a", Locale.ENGLISH);
targetFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
Date date = originalFormat.parse("2014-10-11T17:00:41+0000");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: Oct 11, 2014 5:00 PM
Use SimpleDateFormat for parse input string and represent in new format:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Ex.:
SimpleDateFormat sdfmtIn = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat sdfmtOut= new SimpleDateFormat("dd-MMM-yyyy");
java.util.Date date = sdfmtIn.parse(strInput);
String strOutput = sdfmtOut.format(date);

How to convert current date time from IST to EST timezone in Android?

How to get the EST timzone date (already convrted and get String) into date format with its Timezone?
I tried with changing timezone and I got the perfect datetime in string format but while I am parsing that String date into date format it gives me local Android system timezone time.
Thanks for any help...in advance.
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm aa");
TimeZone tz = TimeZone.getTimeZone("EST");
outputFormat.setTimeZone(tz);
String current_date = outputFormat.format(date);
Date currentdate = outputFormat.parse(current_date);
It give's me EST date format in String (current_date).
It give's me IST format date (currentdate)
You are actually reversing the output from the string back to its original timezone, so when you format the Date from IST to EST it will give you the right specified timezone but by the time you parse it will revert it back to its current timezone the phone is currently using.
solution:
use another instance of SimpleDateFormat and use that to parse the string to prevent it to be reverted back to its current timezone
sample:
Date date = new Date();
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm aa");
TimeZone tz = TimeZone.getTimeZone("IST");
outputFormat.setTimeZone(tz);
String current_date = outputFormat.format(date);
SimpleDateFormat outputFormat2 = new SimpleDateFormat("yyyy-MM-dd hh:mm aa");
Date currentdate = outputFormat2.parse(current_date);

How to Convert SQL TimeStamp in this format "1394039043000" to String

I am using the method below but there is an hour difference in the converted timestamp
public static String getServerFormattedDate(String dateStr) {
Timestamp ts = new Timestamp(Long.valueOf(dateStr)); // input date in
// Timestamp
// format
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(ts);
cal.add(Calendar.HOUR, +7); // Time different between UTC and PDT is +7
// hours
String convertedCal = dateFormat.format(cal.getTime()); // This String
// is converted
// datetime
/* Now convert String formatted DateTime to Timestamp */
return convertedCal;
}
Don't do timezone math on the timestamp yourself. Instead, keep it in UTC and set the timezone on the DateFormat. For example:
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = new Date(Long.valueOf(dateStr));
String convertedCal = dateFormat.format(date);
By default SimpleDateFormat uses timezone settings appropriate for the current default locale and that can explain the 1 hour difference you're seeing.

How to create a Joda DateTime from a String

I have a String "26/09/2012 07:30:00" is it possible to create a new DateTime based on this String? Eventually i just want the time eg 7:30. I am going to format the DateTime by using DateFormatter eg
DateTimeFormatter fmt = DateTimeFormat.forPattern("k,m");
My question is how to construct a DT from the orginal String, i can format it once it's a DT.
Create an appropriate DateTimeFormatter for the input format, and then call DateTimeFormatter.parseLocalDateTime. (LocalDateTime is more appropriate than DateTime here, as your input data doesn't have a time zone or UTC offset indicator. You can convert to DateTime if you really need to, but it sound like you don't.)
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = df.format(c.getTime());
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
// Now we display formattedDate value in TextView
TextView txtView = new TextView(this);
txtView.setText("Current Date and Time : "+formattedDate);
txtView.setGravity(Gravity.CENTER);
txtView.setTextSize(20);
setContentView(txtView);

Categories

Resources