I'm trying to set a date format, but when i run this code
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
System.out.println("datefield = "+date);
i take result:
oldstring = 2013-01-1
datefield = Tue Jan 01 00:01:00 MSK 2013
Why datefield isn't equal 2013-01-1?
At first mm in yyyy-mm-dd mean minute not Month. to set month use MM.
It would be look like this :
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
UPDATE
Try this:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
String sdf = new SimpleDateFormat("yyyy-mm-dd").format(date);
System.out.println("datefield = "+sdf);
If you don't use new SimpleDateFormat("yyyy-mm-dd").format(date);
you getting standard date format which include all info. If you want special format you need to use
new SimpleDateFormat("yyyy-mm-dd").format(date);
Also read this article about date formatting
The type of datefield is Date, so the toString method will basically always return the same format, as you are not overriding it.
So what you need to do, is basically:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(oldstring);
System.out.println("datefield = "+date);
String outDateStr = sdf.format(date);
System.out.println("newstring = "+outDateStr);
Use MM for month. mm is for minutes
Related
Need to format this String 2017-08-01T15:43:45+0530 to 2017-08-01T15:43:45+05:30 using a particular date format. Tried with yyyy-MM-dd'T'HH:mm:ssZZ. Did not work..
Date modified = aemPage.getProperties().get(cq:lastModified, Date.class);
private DateFormat seoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZ");
String formattedDate = seoDateFormat.format(modified));
You need three X to get off set like
Sign TwoDigitHours : Minutes
I suggest to use OffsetDateTime if you are working with java8 or higher:
String input = "2017-08-01T15:43:45+0530";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
OffsetDateTime offsetDateTime = OffsetDateTime.parse(input, parser);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
System.out.println(offsetDateTime.format(formatter)); // 2017-08-01T15:43:45+05:30
Since 2017-08-01T15:43:45+05:30 is ISO_OFFSET_DATE_TIME, you can also just use:
String outPut = offsetDateTime.toString();
Update:
If you want to use SimpleDateFormat, try:
String input = "2017-08-01T15:43:45+0530";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
System.out.println(formatter.format(date));
But this only works when your system's offset is 0530 since Date does not hold time zone information.
I have some string respone date like this:
2018-11-30 12:00:00
and i want just get the time, so i want like this:
12.00
and my question, can we delete some character like that?
Sounds like you want to use SimpleDateFormat so something like:
Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String folderName = formatter.format(today);
Based on your comment try:
int index = time.indexOf(‘:’);
int start = index -2;
int end = index + 2;
String newTime = time.substring(start,end);
String apiDateString = "2018-11-30 12:00:00"; // in this case, your string from api
Date apiDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(apiDateString); // convert api date string to date
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String yourDateString = formatter.format(apiDate);
I think you want this.
Edited with little comment.
convert string to date. (apiDateString to apiDate)
create new format for what you want to form. In this case, 12.00
convert apiDate to your format. (apiDate -> 12.00)
You can try this
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
String strDate = "Current Time : " + mdformat.format(calendar.getTime());
display(strDate);
You can use SimpleDateFormat to get time and then replace : by .
I hope it can help your problem!
You can refer to this post.
Use SimpleDateFormat to format you date.
From String to Date
// Convert from string to date
String yourDate = "2018-11-30 12:00:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(yourDate);
//Use calender instead of date to getHours and minute, due to deprecated
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.get(Calendar.HOUR);
calendar.get(Calendar.MINUTE);
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)+"."+calendar.get(Calendar.MINUTE)); //12.0
Having trouble in the following code. The output is dateStr: 11-Jan-11. Can anyone tell me why the date is modified?
String dateStr="";
String actionCompletionDueDate = "16/11/2011";
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
DateFormat destDf = new SimpleDateFormat("dd-MMM-yy");
if(actionCompletionDueDate != null && !actionCompletionDueDate.equals("")) {
// parse the date string into Date object
System.out.println("actionCompletionDueDate: " + actionCompletionDueDate);
Date actionCompletionDate = srcDf.parse(actionCompletionDueDate);
dateStr = destDf.format(actionCompletionDate);
System.out.println("dateStr: " + dateStr);
}
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("dd/MM/yyyy");
OR pass a correct string (which respects your format) to your code
String actionCompletionDueDate = "11/16/2011";
and correct the format to DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
mm is for minutes and MM is for months
String actionCompletionDueDate = "16/11/2011";
Should be
String actionCompletionDueDate = "11/16/2011";
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
small mm here corresponds to minute.
But if you print source date using,
System.out.println(actionCompletionDate.toString());
Output is :
Sun Jan 16 00:11:00 IST 2011
See, 11 minute in time.
And change source date too, to 11/16/2011.
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+d);
I get the output as
date in controller Mon Dec 31 16:04:57 IST 2012
Please suggest a method to output the date in MM/dd/yyyy HH:mm:ss format.
Need the output in date format and not as string so as to store the output in datetime field in mysql db
Need the output in date format and not as string so as to store the output in datetime field in mysql db
After the statement
Date d = (Date)formatter.parse(dateTime);
java.sql.Date sqldate = new java.sql.Date(d.getTime())
you have got a java.util.Date object and you can store it as it is in mysql DB (column type : datetime).
However, when you are printing d, it defaults to the .toString() implementation. I think you expected some output like Date# but the toString printed in user readable format thats why the confusion.
You are using d an object of Date class, so its toString method is called that gives the output as Mon Dec 31 16:04:57 IST 2012.
If you want to display it in the format that you have specified in your SimpleDateFormat then try using this :
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+ formatter.format(d));
Don't see why the single-qoutes (') are used in the format-string and you also need to catch ParseException:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d = new Date();
try {
d = (Date)formatter.parse("12/31/2012 12:13:14");
} catch(ParseException pex) { System.out.println(pex.getMessage()); }
// convert the date into java.sql.Date
java.sql.Date sqldate = new java.sql.Date(d.getTime());
// then put it in the database, something like this:
//resultSet.updateDate("myDateTimeField", sqldate);
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
System.out.println("date in controller"+ df.format(d));
Try using format instead of parse.
Date date = new Date();
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date) );
how would you write te date if i have a date and all i want is the month and the day like this (mm/dd) and then turn the month like this July, 08
Let me see if I understood well.
You have a date like "07/08" and you want "July, 08"?
You could try SimpleDateFormat
import java.text.SimpleDateFormat;
import java.text.ParseException;
class Test {
public static void main( String [] args ) throws ParseException {
SimpleDateFormat in = new SimpleDateFormat("MM/dd");
SimpleDateFormat out = new SimpleDateFormat("MMMM, dd");
System.out.println( out.format( in.parse("07/08") ) );
// Verbose
//String input = "07/09";
//Date date = in.parse( input );
//String output = out.format( date );
//System.out.println( output );
}
}
Use:
Format formatter = new SimpleDateFormat("MMMM, dd");
String s = formatter.format(date);
Formatting a Date Using a Custom Format
The SimpleDateFormat is your friend here. If you already have a java.util.Date object, just format it using the desired pattern (refer to the javadoc for details on date and time patterns):
SimpleDateFormat out = new SimpleDateFormat("MMMM, dd");
String s = out.format(date); // date is your existing Date object here
(EDIT: I'm adding some details as the original question is unclear and I may have missed the real goal.
If you have a String representation of a date in a given format (e.g. MM/dd) and want to transform the representation, you'll need 2 SimpleDateFormat as pointed out by others: one to parse the String into a Date and another one to format the Date.
SimpleDateFormat in = new SimpleDateFormat("MM/dd");
Date date = in.parse(dateAsString); // dateAsString is your String representation here
Then use the code snippet seen above to format it.)
the month and the day like this (mm/dd) and then turn the month like this July, 08
So you want to convert MM/dd to MMMM, dd? So you start with a String and you end up with a String? Then you need another SimpleDateFormat instance with the first pattern.
String dateString1 = "07/08";
Date date = new SimpleDateFormat("MM/dd").parse(dateString1);
String dateString2 = new SimpleDateFormat("MMMM, dd").format(date);
System.out.println(dateString2); // July, 08 (monthname depends on locale!).