converting excel date into mm/dd/yyyy format
Your Date Format String is wrong. Should be like following
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Read the Java doc for SimpleDateFormat
Convert using excel method numeric value in date - java
LocalDate msBaseDate = LocalDate.of(1899, Month.DECEMBER, 30);
int[] integers = { 4305, 4304, 4303 };
for (int integerValue : integers) {
LocalDate date = msBaseDate.plusDays(integerValue);
System.out.println(date);
}
Output from this snippet is:
1911-10-14
1911-10-13
1911-10-12
Excel and other Microsoft products and possibly other software too use December 30, 1899, as a base date and represent dates as a count of days since that date. So just add the number to that date. I am using LocalDate
from java.time, the modern Java date and time API.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Converting Excel Date Serial Number to Date using Javascript
Try this:
function ExcelDateToJSDate(serial) {
var utc_days = Math.floor(serial - 25569);
var utc_value = utc_days * 86400;
var date_info = new Date(utc_value * 1000);
var fractional_day = serial - Math.floor(serial) + 0.0000001;
var total_seconds = Math.floor(86400 * fractional_day);
var seconds = total_seconds % 60;
total_seconds -= seconds;
var hours = Math.floor(total_seconds / (60 * 60));
var minutes = Math.floor(total_seconds / 60) % 60;
return new Date(date_info.getFullYear(), date_info.getMonth(), date_info.getDate(), hours, minutes, seconds);
}
Custom made for you :)
change date 5 digits int to date string in java
Excel’s serialized dates are the number of days since 1/1/1900. In order to figure out the date again, we have to add the serial number worth of days.
Java 8 version
/*
1900-1-0 0
1900-1-1 1
1900-1-2 2
1900-1-3 3
*/
int days = 43323;
LocalDate start = LocalDate.of(1900, 1, 1);
LocalDate today = LocalDate.of(2018, 8, 11);
// days to date
LocalDate date = start.plusDays(days).minusDays(2);
System.out.println(date);
// date to days
long days1 = ChronoUnit.DAYS.between(start, today) + 2;
System.out.println(days1);
How can I parse this numbers to timestamp?
Excel represents datetime as a decimal number in which the integral represents the number of days since the epoch of 0 January 1900 (or properly 31st December 1899) and the fractional portion represents time as a fraction of a day.
Helpfully, Excel erroneously assumes that the year 1900 is a leap year.
There are 86400 seconds in a day.
With this knowledge it is fairly straightforward to write a function that parses an Excel datetime representation to a LocalDateTime
object within Java.
import java.time.LocalDateTime;
public class ExcelParser
{
public static void main(String[] args)
{
String s = "43706.4856597222";
LocalDateTime testDate = parseExcelDate(s);
System.out.println(testDate);
}
public static LocalDateTime parseExcelDate(String s)
{
long days = Long.parseLong(s.split("\\.")[0]); // Get days from epoch as long
double seconds = (Double.parseDouble(s) - days) * 86400; // Get time fraction and convert to seconds
if (days >= 59)
{
days--; // Shave extra day if days push date past erroneous leap day in 1900
}
// Construct and return LocalDateTime object
return LocalDateTime.of(1899, 12, 31, 0, 0, 0).plusDays(days).plusSeconds((long) Math.ceil(seconds));
}
}
Output
2019-08-29T11:39:21
Obviously the above is just an example solution and does not feature Exception
handling, but it should give you an idea of how you might go about solving this problem.
Convert date from excel in number format to date format python
from datetime import datetime
excel_date = 42139
dt = datetime.fromordinal(datetime(1900, 1, 1).toordinal() + excel_date - 2)
tt = dt.timetuple()
print(dt)
print(tt)
As mentioned by J.F. Sebastian, this answer only works for any date after 1900/03/01
EDIT: (in answer to @R.K)
If your excel_date
is a float number, use this code:
from datetime import datetime
def floatHourToTime(fh):
hours, hourSeconds = divmod(fh, 1)
minutes, seconds = divmod(hourSeconds * 60, 1)
return (
int(hours),
int(minutes),
int(seconds * 60),
)
excel_date = 42139.23213
dt = datetime.fromordinal(datetime(1900, 1, 1).toordinal() + int(excel_date) - 2)
hour, minute, second = floatHourToTime(excel_date % 1)
dt = dt.replace(hour=hour, minute=minute, second=second)
print(dt)
assert str(dt) == "2015-05-15 00:13:55"
Related Topics
Connect to Remote MySQL Database Through Ssh Using Java
Java Cut String After 2 Decimal Digits
How to Subtract Number of Days from Current Date in Hql Query
Selenium Chromedriver Opens a Blank Page Instead of Url on Linux
Android Webview Displaying Blank Page
Regex to Match Words of a Certain Length
How to Show Alert in a Jsp from a Servlet and Then Redirect to Another Jsp
Javac Option to Compile All Java Files Under a Given Directory Recursively
Refreshing Existing Windows in Java Swing
Enable Jackson Deserialization of Empty Objects to Null
Newline Character in Jlabel.Settext()
Java: Print Elements of Array and Insert Line Break
Add Additional Rules to the Compare Method of a Comparator
Efficient Way of Processing Large CSV File Using Java
At Runtime, Find All Classes in a Java Application That Extend a Base Class
Notifydatasetchanged() Makes the List Refresh and Scroll Jumps Back to the Top