Common Questions with Date Format
1. What are the possbile date formats?
2. Can I use any delimiter?
I couldn't find a concrete explanation of the possible date formats but I decided to write one based on trial and and error.
Date dateNow = new Date();
a.
System.out.println(dateNow);
Output: Fri Oct 19 09:26:59 CST 2012
b.
String dateStr = new SimpleDateFormat("MMM/dd/yyyy").format(dateNow);
System.out.println(dateStr);
Output: Oct/19/2012
Use MMM to get Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
Note: If the output month is not in the format "Oct", you can try adding Locale.ENGLISH:
String df = new SimpleDateFormat("MMM/dd/yyyy", Locale.ENGLISH).format(dateNow);
c.
String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(dateNow);
System.out.println(dateStr);
Output: 2012-10-19
d.
String dateStr = new SimpleDateFormat("yyyy/mm/dd").format(dateNow);
System.out.println(dateStr);
Output: 2012/38/19
Note : In letter D, the output is supposedly 2012/10/19. However, you are using the format "yyyy/mm/dd". The month variable should be in capitals, MM instead of mm.
Note : Also make sure that your year variable are in small letters, yyyy instead of YYYY. It will return an error.
As you can see, the format can be interchangeable.
2. Can I use any delimiter? Yes.
Thursday, 18 October 2012
Thursday, 11 October 2012
Javascript Tricks: Getting the full filepath in Mozilla FF and IE.
During our project, I stumbled upon a problem that seemed unsolveable. I was required to get the full path name:
C:\Documents and Settings\Clare\My Documents\test.jpg
My html code was:
<input id="uploadedFile" name="uploadedFile" type="file" onchange="readFile();">
<input type="hidden" value="" id="uploadFileName"/>
My Javascript/Jquery code:
function readFile() {
var fullPath = $('#uploadedFile').val();
if (fullPath != null) {
alert(fullPath );
$('#uploadFileName').val(fullPath ); //Transfer the file path to hidden field
}
However, this only works with IE. And we mainly use Firefox.
In firefox,
The output is: test.jpg
Note: For security reasons, FF prevents the server to access local information.
In IE,
The output is: C:\Documents and Settings\Clare\My Documents\test.jpg
which is correct.
I have been searching for the solution for almost 8 hours. I have tried using an applet but it didn't work(Probably done it the wrong way). Until I found this 100% working solution:
I forgot the website, but if you are the owner of this code, pls don't hesitate to send me a msg.
Complete html code:
<input id="uploadedFile" name="uploadedFile" type="file" onchange="readFile(this);">
<input type="hidden" value="" id="uploadFileName"/>
Code:
C:\Documents and Settings\Clare\My Documents\test.jpg
My html code was:
<input id="uploadedFile" name="uploadedFile" type="file" onchange="readFile();">
<input type="hidden" value="" id="uploadFileName"/>
My Javascript/Jquery code:
function readFile() {
var fullPath = $('#uploadedFile').val();
if (fullPath != null) {
alert(fullPath );
$('#uploadFileName').val(fullPath ); //Transfer the file path to hidden field
}
However, this only works with IE. And we mainly use Firefox.
In firefox,
The output is: test.jpg
Note: For security reasons, FF prevents the server to access local information.
In IE,
The output is: C:\Documents and Settings\Clare\My Documents\test.jpg
which is correct.
I have been searching for the solution for almost 8 hours. I have tried using an applet but it didn't work(Probably done it the wrong way). Until I found this 100% working solution:
I forgot the website, but if you are the owner of this code, pls don't hesitate to send me a msg.
Complete html code:
<input id="uploadedFile" name="uploadedFile" type="file" onchange="readFile(this);">
<input type="hidden" value="" id="uploadFileName"/>
Code:
Subscribe to:
Posts (Atom)