Converting date: d-M-yyyy to dd-MM-yyyy [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Conversion of Date
I get the date from dialog as: 5-1-2012 - as a String.
I need to convert it to 05-01-2012, string as well. What is the simpliest way to do it?

Do you mean?
String date = "5-1-2012";
if (date.charAt(1) == '-') date = "0" + date;
if (date.charAt(4) == '-') date = date.substring(0,3) + "0" + date.substring(3);
// date is 05-01-2012

SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy");
String format = s.format(new Date());

If you plan to use it as a Date object later, I would use the SimpleDateFormat parse() function...
you may test your string to determine whether to use a d-M-yyyy or dd-MM-yyyy pattern for the parser

Related

convert epoch json format string to date on java [duplicate]

This question already has answers here:
How to convert Timestamp into Date format in java
(4 answers)
Closed 2 years ago.
I have a string I recieve from service is a date, this date have this format "/Date(1607490140063-0600)/" is a string, now I need convert from a string to a date format on java, I try differents ways but no work
String endTime = getPunchTime();
SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z");
Date d2 = timeFormat.parse(endTime);
Write a helper method for parsing the value, and use regex to parse the text, e.g. like this:
static OffsetDateTime parseJsonDate(String s) {
Pattern p = Pattern.compile("/Date\\((-?\\d+)([+-]\\d{4})\\)/");
Matcher m = p.matcher(s);
if (! m.matches())
throw new DateTimeParseException("Not a valid JSON Date string: " + s, s, 0);
return Instant.ofEpochMilli(Long.parseLong(m.group(1))).atOffset(ZoneOffset.of(m.group(2)));
}
Test
System.out.println(parseJsonDate("/Date(1607490140063-0600)/"));
Output
2020-12-08T23:02:20.063-06:00

How i can get date format of any datetime string? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Closed 4 years ago.
I receive this "10/1/2018, 1:27:42 PM" as date from server. Now want to convert it to SimpleDateFormat("yyyy-MM-dd HH:mm:ss"). But to do this i need to know the format of existing date string.
How i can find a correct format of "10/1/2018, 1:27:42 PM"?
You need to use SimpleDateFormat.toPattern() to get the pattern. Use this function in this way -
SimpleDateFormat sdf = new SimpleDateFormat();
// get the current date and time
Calendar cal = Calendar.getInstance();
String dateToday = sdf.format(cal.getTime());
// get the pattern used by the formatter and print it
String pattern = sdf.toPattern();
System.out.println("Pattern:"+pattern);

Java - Converting yyyy-MM-dd'T'HH:mm:ssZ to readable dd-MM-yyyy [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
I will have a String input in the style of:
yyyy-MM-dd'T'HH:mm:ssZ
Is it possible to convert this String into a date, and after, parsing it into a readable dd-MM-yyyy Date Object?
Yes. It can be done in two parts as follows:
Parse your String to Date object
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date dt = sd1.parse(myString);
Format the Date object to desirable format
SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
String newDate = sd2.format(dt);
System.out.println(newDate);
You will have to use two different SimpleDateFormat since the two date formats are different.
Input:
2015-01-12T10:02:00+0530
Output:
2015-01-12
//Parse the string into a date variable
Date parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString);
//Now reformat it using desired display pattern:
String displayDate = new SimpleDateFormat("dd-MM-yyyy").format(parsedDate);

Changing the date format from mm/dd/yyyy [duplicate]

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 7 years ago.
I need to change the date format from 2015-04-08 to 08-APR-2015.
It is coming from grails gsp front end.
Before calling oracle package I need to change the format in java.
How to do it.
I use SimpleDateFormat in changing date formats. Try this:
String OLD_FORMAT = "yyyy-MM-dd";
String NEW_FORMAT = "dd-MMM-yyyy";
String oldDateString = "2015-04-08";
String newDateString;
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);
You mentioned Java in your answer but you tagged JavaScript I've edited your question to show the Java tag. This is a JavaScript soution.
Assuming str is you input:
str.split('-').forEach(function(a,i,b){
result += i===0?(b[2]+'-'):i===1?(((new Date(str)).toUTCString()).split(' ')[2]+'-').toUpperCase():a[0];
});
Ok I already see and answer but still want to add one.I am not sure you want JS or Java solution. You can this using Javascript or Java. Below are both ways:
Javascript
var c = new Date('2015-04-08');
locale = "en-us"
function formatDate(d)
{
var month = d.toLocaleString(locale, { month: "long" });
var day = d.getDate();
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
return day + '-' + month +'-' + d.getFullYear();
}
And if you want same in Java you do below..
Java
String myDateString = "2015-04-08";
String reqDateString = new SimpleDateFormat("dd-MMM-yyyy").format(myDateString);
Better if done in Java looks more simpler and reliable..

How to convert Date represented as a String to milliseconds? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert java string to Date object
In my Java code I have input String like 05.10.2011 which I need to convert to milliseconds.
So:
String someDate = "05.10.2011";
I have to convert to match milliseconds format.
String someDate = "05.10.2011";
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
Date date = sdf.parse(someDate);
System.out.println(date.getTime());
You can use Java's SimpleDateFormat to easily convert to a Date instance.
SimpleDateFormat formatter = new SimpleDateFormat("MM.dd.yyyy"); // Month.Day.Year
Date d = formatter.parse(inputString);
long timestamp = d.getTime();
use the simpleDateFormat which takes a string and converts it to Date then call the getTime() to get the date in milliseconds format

Categories

Resources