I have a question, how can I convert a string like 20130706123020 to a date object.
So I what to convert the string 20130706123020 to a date object looking like:
2013-07-06 12:30:20
Attempted code:
String date = "20130706234310";
Date date1 = new SimpleDateFormat("yyyy-m-d H:m:s").parse(date);
System.out.println(date1);
Any suggestions will be appreciated.
Thank you!
You have to first parse the String using the parse method from SimpleDateFormat.
Then pass the Date object returned by the parse method to another SimpleDateFormat and then using the format method get the date in the format you want.
String s = "201307061230202";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS"); // format in which you get the String
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // format in which you want the date to be in
System.out.println(sdf1.format(sdf.parse(s)));
The significance of HH, hh, KK and kk in the hour field is different. I have used HH you can use the one according to your requirement.
H Hour in day (0-23)
k Hour in day (1-24)
K Hour in am/pm (0-11)
h Hour in am/pm (1-12)
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = sdf.parse("20130706123020");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf2.format(date));
use this :
long int my_date = 20130706123020L
and after that :
String date_Text = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(my_date));
Related
I am writing in a CSV and my calendar is doubling values ... I couldn't figure out the problem.
PS: Amount is like 1.000.000 or 10.000.000.
public static void CSV(String path, int amount) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.set(1980, 01, 01);
for (; set.size() < amount;) {
c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 2);
set.add(c.getTime());
}
Iterator<Date> it = set.iterator();
for (int i = 0; i < amount; i++) {
csvWriter.append(dateFormat.format(it.next()));
}
...
}
Well, the error was the Hour in am/pm (1-12).
Thanks to #Teemu.
I assume you mean "doubling values" by that the same time occurs twice. Reason for that is your date formatter:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
You are formatting hours as 'hh'. Meaning that it is formatting date as Hour in am/pm (1-12) . So two time values are actually unique, one is AM and another is PM. You are not providing the AM / PM markings into SimpleDateFormat and that's why both time values looks the same.
If you want to distinquish the AM / PM markings change the format to this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aaa");
Or another way is to format hours in 0-23 format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Not sure if my assumption is correct but I hope this helps.
When I convert the String "07/02/2014" (mm/dd/yyy) to a java.util.Date using SimpleDateFormatter I get a result of Sun Dec 29 00:00:00 CAT 2013.
Here is the code I am running:
DateFormat formatter;
formatter = new SimpleDateFormat("MM/DD/YYYY");
Date exactDate = formatter.parse("07/02/2014");
Why is this happening ?
It must be:
DateFormat formatter;
formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
The documentation explains why.
y Year
Y Week year
D Day in year
d Day in month
M Month in year
m Minute in hour
Try this one,
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
When you print the Date object you will get that output. Try formatting Date into String for desired format and output.
Try following code:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date exactDate = formatter.parse("07/02/2014");
String date=new SimpleDateFormat("MM/dd/yyyy").format(exactDate);
Hope this helps.
I have an issue where I would like to get the start of the day, however it seems to be setting it to 12:00 via automatic.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Why is this spitting out:
2014-06-06 12:00:00
The 12:00:00 is the issue
That is because hh represents the hour in 12 hour format. You need to use HH instead.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Have a look at the docs for more info.
Also, on a side note, there is a typo in your code.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date todayDate = dfFull.parse("2014-06-06 00:00:00"); // todayDate must be of type Date and not String
String today = dfFull.format(todayDate); // today should be of type String
System.out.println(today);
You should use HH for hours in this case.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Now you will get the out put as
2014-06-06 00:00:00
And again if you use hh that mean you are using 12 hour format while HH means 24 hour format
So
I want to convert a date object, ex: new Date(), to a string which has a format like Oracle's time stamp type, ex: 21-OCT-13 11.08.13.858000000 AM. I know I could just get each piece of information in the date object like day, month, year, hour, minute, ... to form the Oracle format string but I really want to know is there a utility to do that instead?
Using SimpleDateFormat#format() you would print a Date as
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS a");
System.out.println(sdf.format(new Date()).toUpperCase());
Output :
21-OCT-13 10.01.38.000000614 AM
See JavaDocs for Date and Time patterns.
Try taking a look at SimpleDateFormats - That would be your best bet and easiest way of doing it.
Eg:
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); //Hours:Minutes:Seconds
String strDate = dateFormat.format(date);
Use SimpleDateFormat.
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("your_format_here"); // dd/MM/yy h:mm:ss a
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
String Resultmasterid=res1.getString(1);
System.out.println(Resultmasterid);
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHMM");
Date varDate1=dateFormat1.parse(Resultmasterid);
dateFormat1=new SimpleDateFormat("dd-MMM-yyyy HH:MM");
String Final_admitDT=dateFormat1.format(varDate1);
This is my code I get the date as yyyyMMddHHMM format, now I need to change the format in dd-mmm-yyyy HH:MM. I get the result but it is not correct. Can any one help me on this please.
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHmm");
SimpleDateFormat("dd-MMM-yyyy HH:mm");
instead of
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHMM");
SimpleDateFormat("dd-MMM-yyyy HH:MM");
SimpleDateFormat
MM indicates month
mm indicates minutes
You need to use
dateFormat1=new SimpleDateFormat("dd-MMM-yyyy HH:mm");
because mm represent the Minute in hour, whereas MM represent Month in year.
Have a look at the docs of SDF for more info on the patterns and pattern letters.
Your patterns are wrong, you should use MM for month and mm for minutes:
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMddHHmm");
...
dateFormat1 = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
By the way, you should also respect the Sun code conventions and not use local variables starting with a capital letters (e.g. finalAdmitDT instead of Final_admitDT).