I have to convert a java string to date in vbscript with given locale. below are some of date string
Dim dstr,dstr2
dstr= 30 Oct 2013 07:49:37 GMT
dstr2 =30-Oct-2013 13:20:22 India Standard Time
From these if I remove there locale then below syntax work
CDate(replace(dstr,"GMT"),"")
CDate(replace(dstr,"India Standard Time"),"")
But it does not give Locale. can any body tell me how can I fetch date with local given in the string.
I'm not sure whether I got your question correctly or not but here goes:
Dim dstr, dstr2, intRegionalCode
intRegionalCode = GetLocale()
dstr = "30 Oct 2013 07:49:37 GMT"
dstr2 = "30-Oct-2013 13:20:22 India Standard Time"
SetLocale("en-gb")
MsgBox CDate(replace(dstr,"GMT",""))
MsgBox CDate(replace(dstr2,"India Standard Time",""))
SetLocale("en-us")
MsgBox CDate(replace(dstr,"GMT",""))
MsgBox CDate(replace(dstr2,"India Standard Time",""))
SetLocale(intRegionalCode)
I hope this helps.
For more information you can check here.
GetLocale help
Related
hello guys I've been on this issue for 4 hours and I don't seem able to solve it.
I want the date selected using JDateChooser to be converted to this format dd-mm-yyyy but I couldn't do this.
This what I wrote but it prints the date as :Thu Aug 21 00:00:00 WEST 2014. I want it to be printed 21-08-2014
This is my code:
public void addIns() {
JTextField ch=(JTextField) dateChooser.getDateEditor().getUiComponent();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
try{
Date date = df.parse(ch.getText());
} catch(Exception e){System.out.println("wrong date");}
System.out.println(date); }
The format Thu Aug 21 00:00:00 WEST 2014 is the default format of the Date.toString method.
So, if you are expecting:
System.out.println(date);
To print 21-08-2014, then you are wrong.
You could:
in Java 8 use LocalDate and LocalTime, and have a decent toString (but that won't print 21-08-2014).
print the date with the format method, eg: df.format(date);
And, since you are using Swing - and Swing components - there is probably a better way to get or set the format on the component.
I have followed this SO answer for datetime conversion of 8601.
I will cite an example straight from w3 :
1994-11-05T08:15:30-05:00 corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.
1994-11-05T13:15:30Z corresponds to the same instant.
And this is what I run in android
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
dateTime = sdfSource.parse("2014-03-06T11:30:00-05:00");
System.out.println(dateTime); //Thu Mar 06 18:30:00 EET 2014
Obviously .parse()'s output is the local aware datetime. There has been a conversion from EST(-05:00) to EET (+02:00) since now I am in this timezone. However I do not want this auto-convertion.
Is there a way to parse a datetime string inyyyy-MM-dd'T'HH:mm:ssZZZZZ format and display THAT timezone's datetime? Preferable output:
Thu Mar 06 11:30:00 EST 2014
The EST and my location is an example. It can be any other timezones as well.
Internally Date objects are in UTC and that's what they're parsed to.
You cannot retrieve the original timezone from the Date but you can attempt to retrieve it from the original ISO-8601 stamp, and use it when formatting.
When you convert it to a string with toString(), it uses your local settings to format the date. If you want a specific representation, use a formatter to format the output, e.g.
int rawTimeZoneOffsetMillis = ...; // retrieve from ISO-8601 stamp and convert to milliseconds
TimeZone tz = new SimpleTimeZone(rawTimeZoneOffsetMillis, "name");
DateFormat outputFormat = DateFormat.getDateTimeInstance();
outputFormat.setTimeZone(tz);
System.out.println(df.format(dateTime));
ISO-8601 timestamps are not completely parseable with SimpleDateFormat. This answer has some code to work around some of the limitations.
Use sdfSource.setTimeZone() method
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
sdfSource.setTimeZone(TimeZone.getTimeZone("EST")); //give the timezone you want
dateTime = sdfSource.parse("2014-03-06T11:30:00-05:00");
System.out.println(dateTime); //Thu Mar 06 18:30:00 EET 2014
This should do fine.
Although you should not be worried while parsing the date as it is parsed to correct value can be displayed in any format or timezone you want.
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
sdfSource.setTimeZone( TimeZone.getTimeZone( "EST" ) );
dateTime = sdfSource.parse("2014-03-06T11:30:00-05:00");
System.out.println(sdfSource.format(dateTime)); //Thu Mar 06 18:30:00 EET 2014
I have a Date object as follows:
java.util.Date d = new java.util.Date(System.currentTimeMillis()); // Mon Dec 23 14:57:28 PST 2013
I need to format the date to get another Date object with this format instead:
2013-12-23 14:57:28
I tried this code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
sdf.format(d); // d is still Mon Dec 23 14:57:28 PST 2013, no formatting.
I tried this code:
String s = d.toString();
try {
d = sdf.parse(s);
} catch (Exception e)
e.printStackTrace(); // java.text.ParseException: Unparseable date: "Mon Dec 23 14:35:48 PST 2013"
Would you please tell me what am I doing wrong? I googled searched it but the solutions to format a Date was more or less what I tried. Any help is greatly appreciated.
You don't understand what a Date is, and what format() does. A Date is just a number of milliseconds. Nothing more. It doesn't have any format. Formatting a date doesn't change the date at all. It returns a string containing a human readable representation of the date (like "2012-11-23" or "Monday, April 2").
So, the following instruction:
sdf.format(d);
is effectively a noop. You ignore the string that it returns.
If what you want is to have a specific format used when calling date.toString(), it's impossible. When you want to display a date in a specific format (yyyy-MM-dd for example), instead of doing
System.out.println(date);
use
DateFormat format = new SimpleDaeFormat("yyyy-MM-dd");
System.out.println(format.format(date));
All this is clearly explained in the javadoc. You should read it.
SimpleDateFormat, does not change the date format, it gives you a formatted date for display purpose only, not for anything else.
util.Date will always have one format (a long number of milliseconds) that you can format to any way you want in order to display using SimpleDateFormat. So in effect no matter what date you get you can format to what format you want.
If you explain why you are trying to do what you are trying to do, then maybe we can support you better.
I'm using Flex+java+Mysql.. in that i want to insert the datetime taking from flex to mysql in that i got date String like this
Sat Aug 4 12:05:00 GMT+0530 2012
how to convert yyyy-MM-dd HH:mm:ss z
this formate so please give code in java or flex...
In flex API Given DateFile class, in that we have two methods dateTOString(), stringTODate().
DateField.dateToString(dateInput.selectedDate,"DD-MM-YYYY");
Please Refere Below Doc.
DateField
DateField.dateToString(addr1.selectedDate,"YYYY-MM-DD").toString();
What is the proper date format for this date instance ..
10/10/2011 2:36:00 PM
I've used this ..
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
edit
more code
Object temp = jsonArray.getJSONObject(i).get("STARTDATE"); // date object from webserive
Date appointment.mStartDate = formatter.parse(temp.toString());
but it returned this date in this format ..
Thu Nov 10 00:36:00 GMT+02:00 2011
but it returned this date in this format ..
Thu Nov 10 00:36:00 GMT+02:00 2011
You were thus doing System.out.println(appointment.mStartDate);? That's then perfectly fine. It's indeed the default format of the Date#toString() method. When you pass a non-String object to System.out.println(), then its toString() method will be called and the returned String will be displayed.
If you want to display it in the same format as you have retrieved it, then you should be using the SimpleDateFormat#format() method to convert Date to a String in the desired format:
String dateString = formatter.format(appointment.mStartDate);
System.out.println(dateString);
Use parseObject
Date appointment.mStartDate = (Date) formatter.parseObject(temp.toString());
There is four acknowledged variants:
US M/D/YY
ISO-8601 YYYY-MM-DD
JIS ?
EUR DD.MM.YYYY
What's your problem? The date appears to have been parsed correctly for a timezone of +2 hours. Naturally, when you simply print the toString description, the displayed value is in the default format -- to format the output you need to do a date formatter operation.