i have done code below to change time from UTC to another time zone but code is showing only UTC time.Also after formatting to source time format it shows system time zone .
private String setTimezone(String time){
sourceformatter = new SimpleDateFormat("hh:mm a, E dd MMM yyyy");
dateFormatter = new SimpleDateFormat("hh:mm a");
sourceformatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.e("reicievedformat",time);
Date value = null;
try {
value = sourceformatter.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Log.d("afterfirstformat",dateFormatter.format(value));
dateFormatter.setTimeZone(TimeZone.getTimeZone("IST"));
time =dateFormatter.format(value);
Log.d("Finaltime",time);
return time;
}
Output:- Log values
E/reicievedformat: 12:36 PM, Mon 08 Oct 2018
D/afterfirstformat: 06:21 PM
D/Finaltime: 12:36 PM
As you can see I'm getting 12:36 PM, Mon 08 Oct 2018 ("UTC") and I want to convert to IST, but the final time, 12:36 PM, doesn’t seem to have been converted.
IST in java stands for "Israel Standard Time".
Use this for "Indian Standard Time"
dateFormatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
The date & time APIs in Java gives you headache.
I use this library by Daniel Lew.
https://github.com/dlew/joda-time-android
Try this
public static String getDateOut(String ourDate) {
try
{
//be sure that passing date has same format as formatter
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a, E dd MMM yyyy");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = formatter.parse(ourDate);
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm a"); //this format changeable
dateFormatter.setTimeZone(TimeZone.getTimeZone("IST"));
ourDate = dateFormatter.format(value);
}
catch (Exception e)
{
ourDate = "00-00-0000 00:00";
}
return ourDate;
}
Related
Hi everyone i used bootstrap date time picker to save date and time when i select the date in jsp. am getting date and time as a String in java class like this 23 January 2017 - 02:50 pm am trying to convert this to date is not working where i need to change
$(function () {
$('#datetimepicker8').datetimepicker({
startDate: new Date(),
format : 'dd MM yyyy - HH:ii p',
autoclose: 1,
}).on( 'changeDate', function(e) {
// Revalidate the date field
$('#timeTableUpdateForm').bootstrapValidator(
'revalidateField', 'examDate');
});
});
.
public class StringToDate {
public static void main(String[] args) {
SimpleDateFormat formatter =
new SimpleDateFormat("dd MMMM yyyy-HH:mm:ss a");
String dateInString = "23 January 2017 - 02:50 pm";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}
catch( ParseException e ) {
e.printStackTrace();
}
}
}
Here is a working code:
public static void main( String[] args ) {
final SimpleDateFormat formatter =
new SimpleDateFormat( "dd MMMMM yyyy - HH:mm a", Locale.US );
final String dateInString = "23 January 2017 - 02:50 pm";
try {
final Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}
catch( final ParseException e ) {
e.printStackTrace();
}
}
With this output :
Mon Jan 23 02:50:00 CET 2017
23 January 2017 - 02:50 AM
Two errors here:
A space is missing between year and dash - dash and hour
You are telling java to parse seconds, but you don't have seconds in your date string.
You need to change your SimpleDateFormat from
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy-HH:mm:ss a");
To
// Added two spaces and removed seconds from format, added Locale
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy - HH:mm a", Locale.US);
// ^ ^ ^
By changing your code this way I get this output:
Mon Jan 23 02:50:00 GMT 2017
23 January 2017 - 02:50 AM
DEMO
I have a time raw string, "2016-05-15T12:42:00.000-04:00" and I want to convert the string to "Wed 15 May 2016 12:42", which keeps the same timezone (-04:00) as its original source.
I have tried SimpleDateFormat but using it returns different timezones that are not the same as the timezone in my original string. Please help me achieve this in Android Studio!
Other examples:
2016-05-15T15:42:00.000-08:00 -> Wed 15 May 2016 15:42
2016-05-15T14:44:00.000-01:00 -> Wed 15 May 2016 14:44
public static String formatDateString(String originDateString) {
//Original format 2016-05-15T12:42:00.000-04:00
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat resultFormat = new SimpleDateFormat("EEE dd MMM yyyy HH:mm");
String dateString = "";
try {
Date date = originalFormat.parse(originDateString);
dateString = resultFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
//returned format: Tue 14 May 2016 12:42
return dateString;
}
try saving it first without the TZ information
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
I have an Object with the following text:
System.out.println("My date: " + valor);
and it prints
My date: Thu Jan 01 13:00:00 CST 2015
I want to convert this text to a Date variable, this is my code:
dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy",new Locale("es","MX"));
try {
Date dateToString = dateFormat.parse(valor.toString());
} catch (ParseException e) {
new Date();
}
I always get current date, I've tried several combinations for SimpleDateFormat but none works, what's the proper way to convert my Object to a Date variable
Your format doesn't include time zone (z) and you shouldn't swallow an Exception without logging it (which is how I found the error). Something like,
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date dateToString = dateFormat.parse("Thu Jan 01 13:00:00 CST 2015");
System.out.println(dateFormat.format(dateToString));
} catch (ParseException e) {
e.printStackTrace();
}
Output is
Thu Jan 01 13:00:00 CST 2015
Looks like your date format isn't accounting for the timezone. Maybe try
EEE MMM dd HH:mm:ss z yyyy
See Java Docs
I have a date string:
Thu Feb 20 08:00:00 EET 1992
And using this code to format it:
String datePatternFrom = "EEE MMM dd HH:mm:ss ZZZ yyyy";
String datePatternTo = "MMM dd, yyyy";
String prettyDate = "";
try {
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom);
Date date = (Date)fromFormatter.parse(userBirthday.toString());
DateFormat toFormatter = new SimpleDateFormat(datePatternTo);
prettyDate = toFormatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Why I'am getting the exception?
java.text.ParseException: Unparseable date: "Thu Feb 20 08:00:00 EET 1992" (at offset 0)
The problem is with the weekday and month and your locale.
Thu is English, so you have to tell the parser that it should use English weekdays:
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom, Locale.US);
This will work for your pattern.
If you do not specify a locale, the default will be used, which is not always an English one. ;-)
It could be your locale. Try making a SDF with datePatternFrom, give it a date to format and print that somewhere. See what pops up.
Probably your userBirthday object was not created as a java.util.Date object. Can you try a System.out.println(userBirthday.getClass().getName());?
I 'm getting the date in the following format (Date and not String):
Tue Jun 26 07:00:00 EDT 2012
I want to change the format of the date to (Date):
6/26/2012 10:19:15 AM
so as to update the same in the data base. I tried following code:
Date dte;
Date dte1;(Tue Jun 26 07:00:00 EDT 2012)
SimpleDateFormat formatter = new SimpleDateFormat("m/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(dte1);
dte = formatter.parse(formattedDate);
SystemUtils.trace("test", " date>>>" + dte);
is yielding the following response:
Thu Jan 26 07:00:00 EST 2012
Can any one please share the piece of code to do the same asap.
You shouldn't have to format dates to insert them in a database. If using JDBC, use prepared statements.
To answer your question, though, m can't mean minute and month at the same time. M means month. m means minute.
This code outputs needed for you result:
Date dte = new Date();//or something else
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(dte);
System.out.println(formattedDate);
Try this, this below code will suit your need.
public class DateWala {
public static void main(String[] args){
System.out.println(new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").format(new Date()));
}
}
you can use this tiny function
// send your time
private String convertTime(String dateTime) {
//source format will go there
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Date date = null;
try {
date = sdfSource.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
//destination format will go there
SimpleDateFormat sdfDestination = new SimpleDateFormat("dd-MM-yyyy HH:mm");
return sdfDestination.format(date);
}