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.
Related
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
// For Time validation
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
I am working on converting the string which i have into the date and time format. For example, the string datechosen contain "26/10/2020". I am able to to convert it into date format and print it out.
But for the time string, i am unable to print them out. I am facing the error below:
Screenshot of the log message
But if i swap the position of the codes the other way round,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
The time will be printed instead
These are the two input fields
You are setting date in wrong format for time. for cal.setTime(timeselected); setTime takes Date Refer java doc
Use same format as used for Date.
Need to show time in GMT...but it showing in UTC
long m=Long.parseLong("1444588200000");
Date localTime = new Date(m);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
Locale.getDefault());
DateFormat date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
String time = date.format(localTime);
infoip.setText(" "+time);
Try this,
final Date currentTime = new Date();
final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
// Give it to me in GMT time.
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("GMT time: " + sdf.format(currentTime));
Try like this hope work.
Calendar cal = Calendar.getInstance();
// setting the timezone for which you want
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.setTimeInMillis();
// The date is in your home timezone
Date date = cal.getTime();
TimeZone tz = TimeZone.getTimeZone("GMT");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS", Locale.GMT);
sdf.setTimeZone(tz);
// Then we do the conversion to convert the date you provided in milliseconds to the GMT timezone
String result = sdf.parse(date);
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);
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);
I am having a date string 2012-11-21 13:11:25 which I get from local database. I have to convert this according to UTC settings and display it on a particular screen. So if its GMT+05:30 it should be displayed as 2012-11-21 18:41:25 on the screen. How can I do this conversion. I have checked some of the questions but that didn't work out.
I am able to get a Date object that returns something like Wed Nov 21 13:11:25 GMT+05:30 2012 after this I need to get the time as 18:41:25 and date as 11-21-2012
Thanks in advance
Your df and inputFmt must use the same format.
But I think you should do it like this:
Date myDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTime(myDate);
Date time = calendar.getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
String dateAsString = outputFmt.format(time);
System.out.println(dateAsString);
Get UTC from current time :
public String getCurrentUTC(){
Date time = Calendar.getInstance().getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
return outputFmt.format(time);
}
Best way to get formatted string of Date in required format is
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = dateFormat.format(date);
//This is my input date
String dtStart = "2019-04-24 01:22 PM";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
Date date = null;
try {
date = format.parse(dtStart);
getDateInUTC(date)
} catch (ParseException e) {
e.printStackTrace();
}
//This Method use for convert Date into some UTC format
public static String getDateInUTC(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateAsString = sdf.format(date);
System.out.println("UTC" + dateAsString);
return dateAsString;
}