Android joda time convert from 24 to 12 - android

I am inserting the date to database like this:
long d = cal.getTimeInMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String time_string_f=dateFormat.format(d);
time_string_f is the string to insert in database , and the output is like:
07/09/2015 20:47:00
I want to get it from database and format it to be 12 hours with am/pm.
So I got this solution from here
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MM-yyyy hh:mm:ss.SSa");
DateTime jodatime = dtf.parseDateTime(string_date_from_database);
int yy= jodatime.getYear();
I am getting the year just to check if it works.
but it does not work and gives me this error:
07-09 22:34:48.399: I/FFFFF(7165): Invalid format: "07/09/2015 20:47:00" is malformed at "/09/2015 20:47:00"

Value in you database has format MM/dd/yyyy HH:mm:ss - without AM/PM suffix.
So you should parse is without a option and then convert date to 12 hours format.
Example:
String string_date_from_database = "07/09/2015 20:47:00";
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
DateTime jodatime = dtf.parseDateTime(string_date_from_database);
String dateIn12HourFormat = jodatime.toString("MM/dd/yyyy hh:mm:ssa");
// Now 'dateIn12HourFormat' looks like `07/09/2015 08:47:00PM`
You can use simple utility method:
static final DateTimeFormatter hours24 = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
static final DateTimeFormatter hours12 = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm:ssa");
static String convertTo12HoursFormat(String format24hours)
{
return hours12.print(hours24.parseDateTime(format24hours));
}
Usage:
String string_date_from_database = "07/09/2015 20:47:00";
String dateIn12HourFormat = convertTo12HoursFormat(string_date_from_database);

Try like the following.
long d = cal.getTimeInMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
String time_string_f=dateFormat.format(d);

Related

Convert the time stamps into long value

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"

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 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.

Formatting date from a DatePicker

I know I can get the date from a datepicker using the following methods:
datepicker1.getDayOfMonth()
datepicker2.getMonth()
datepicker3.getYear()
I am also getting the time from a timepicker using the following methods:
timepicker1.getCurrentHour()
timepicker2.getCurrentMinute()
I need to format this data into "yyyy-MM-dd HH:mm:ss" format to place it into an sqlite database as a DATETIME variable. Is there a method which can handle this?
I think I will have to build it manually like so:
if(month < 10){
month = "0" + month;
}
if(day < 10){
day = "0" + day ;
}
The problem was that the months and days were single digit.
SimpleDateFormat outputFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date ModifiedDate = outputFormat.parse(strdate);
Simple as below:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.parse(yourDateHere);
You can get Current Date by this following and save it to the string
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
now pass this string to the Simpledateformat class and define your desired Format like following
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(mydate);

Incorrect Result: Formatting ISO-8601 Time And Extract 12 Hour Time

I am fetching Newsfeeds from the Facebook API using FQL which returns a "created_time" field as a UNIX Timestamp. I am converting that into, what I believe is a ISO-8601 timestamp using this piece of code:
String getInitialCreatedTime = JOFeeds.getString("created_time");
long finalTimeStamp = Long.valueOf(getInitialCreatedTime);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
String finalCreatedTime = dateFormatter.format(new Date(finalTimeStamp * 1000L));
Now, from the String, finalCreatedTime I want to extract just the time in 12 Hour (AM/PM) format.
To that effect, I am using this code:
final String old_format = "yyyy-MM-dd'T'HH:mm:ssZ";
final String new_format = "EEE MMM d hh:mm aa yyyy";
String oldDateSring = finalCreatedTime;
SimpleDateFormat sdf = new SimpleDateFormat(old_format);
Date d = sdf.parse(oldDateSring);
sdf.applyPattern(new_format);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
And:
// GET THE TIME
SimpleDateFormat sdfTimeFormatter = new SimpleDateFormat("hh:mm aa");
String getTime = sdfTimeFormatter.format(cal.getTime());
feeds.setTime(getTime);
Log.e("TIME", getTime);
The result of the first code block is: 2012-12-14T04:30:03+0530
And the result from the // GET THE TIME block is 04:30AM when it should be 04:30PM.
I would appreciate any pointers on this. Perhaps, I am implementing it wrong?
EDIT: I might add, that timestamps that fall between 12.00 PM and 1.00 PM are handled properly and show PM as they should.
You have :
String getInitialCreatedTime = JOFeeds.getString("created_time");
long finalTimeStamp = Long.valueOf(getInitialCreatedTime);
// Note 8601 is written with 'HH'
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
So far, so good. But then you need to create a date from this.
Date date = new Date(finalTimeStamp * 1000L)
Then, you need to format what you need (and never EVER parse a date you just formatted. That makes no sense at all).
String finalCreatedTime = dateFormatter.format(date); // Not sure if you need this one
And
SimpleDateFormat sdfTimeFormatter = new SimpleDateFormat("hh:mm aa");
String getTime = sdfTimeFormatter.format(date);
feeds.setTime(getTime);

Categories

Resources