I have to use the MinguoDate in my program that I am writing in Java8.
I have current date in string format:
String currentDate="2018-07-20";
Can anyone tell me how to convert current date to the Minguo date using java8
Note: MinguoDate calendar system is primarily used in Taiwan (Republic of China)
You can parse the string into a LocalDate instance and then put the result of that into the MinguoDate.from method to yield a MinguoDate.
MinguoDate minguoDate = MinguoDate.from(LocalDate.parse(currentDate));
Related
How to convert yyyy-mm-ddThh:mm:ss [+-] hh:mm to timestamp yyyy-mm-dd hh:mm:ss in Datastge?
Eg:
From
2021-01-26T01:07:00-05:00
To
2021-01-26 01:07:00
Depending on the original data type you could use string functions
and StringToTimestamp
Otherwise you could check out Date and Time functions as well.
If the data type is string, convert the "T" to a space " " using Convert() function.
Use Left() function to get rid of the "+/- hh:mm".
If the data type is timestamp, do nothing until you need to display it; the internal representation of a timestamp does not have a "T" in it. If you're writing it to a text file, for example, use TimestampToString() function to specify your desired format, although the default format may well suit your need.
I'm trying to comvert a string in my logs to a date object.
My string is 2018-09-18 11:42:50,286000201 which is the format YYYY-MM-DD hh:mm:ss,nnnnnnnnn
I'm trying to convert in to an object using the time library in ruby. The function I am using is Time.strptime('2018-09-18 11:42:50,286000201, '%Y-%m-%d %H:%M:%S,%9N')
Ruby is giving me an invalid striptime format. Any help would be appreciated. Cheers.
remove the 9, %N expects 9 digits bt default
Time.strptime('2018-09-18 11:42:50,286000201, '%Y-%m-%d %H:%M:%S,%N')
doc: http://ruby-doc.org/stdlib-2.1.1/libdoc/time/rdoc/Time.html#method-c-strptime
I have the following datetime helper method that converts a UTC-zoned Java 8 Date into a datetime string:
public static String dateTimeString(Date date) {
return date.toInstant().atZone(ZoneId.of("UTC")).toLocalDateTime().toString();
}
The desired result is to always have the resultant String be formatted as:
YYYY-MM-dd'T'HH:mm:ss'Z'
Problem is, Java 8 LocalTime#toString() intentionally strips off time components that are zero. So for instance if I have a Date instance that represents June 8, 2018 at 12:35:00 UTC. Then the output of this method above is: 2018-06-08'T'12:35'Z'. Whereas I want it to contain any zeroed-out second/minute/hour components (e.g. 2018-06-08'T'12:35:00'Z').
Any ideas?
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssX");
public static String dateTimeString(Date date) {
return date.toInstant().atOffset(ZoneOffset.UTC).format(formatter);
}
Just use a fixed format pattern string to get your desired format. Let’s try it:
System.out.println(dateTimeString(new Date(0)));
System.out.println(dateTimeString(new Date(1_524_560_255_555L)));
This prints:
1970-01-01T00:00:00Z
2018-04-24T08:57:35Z
In the first example hours, minutes and seconds are printed even if they are 0.
In the second example milliseoncds are omitted even when they are non-zero (you see that the milliseconds value I specified ends in 555).
All of this said, the output conforms to the ISO 8601 format no matter if you have 2018-06-08T12:35Z, 2018-06-08T12:35:00Z or even 2018-06-08T12:35:00.000000000Z. So you may want to check once more whether leaving out the second works for your purpose before you take the trouble of defining your own formatter.
Link: Wikipedia article: ISO 8601
I don't see why you portray the default output to have its letters enclosed in single quotes when Java's actual output instead looks like 2018-06-08T12:35Z, but anyway, here's the code that produces it as desired, with no omissions:
final LocalDateTime ldt = LocalDateTime.of(2018, 6, 8, 12, 35, 0);
final ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.of("Z"));
final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'''T'''HH:mm:ss''X''", Locale.US);
System.err.println(dtf.format(zdt));
Output:
2018-06-08'T'12:35:00'Z'
Personally, I'd probably prefer a format like this, containing the millis, giving time zone information not requiring additional knowledge from the user, and not having imho superfluous characters:
FORMAT: "yyyy-MM-dd HH:mm:ss'.'SSS Z"
OUTPUT: 2018-06-08 12:35:12.345 +0200
It appears that there is no out of the box solution. The simplest way would be to write your own class that extends DateTimeFormatter and override method public String format(TemporalAccessor temporal) where you would call the original method and then if needed modify the output String. See the flags 'Z' for formatter. If that flag is present then you will need to modify the original output.
I've seen that you can use an ".isValid()" function to check that a given string is in a date format:
moment('2007-05-05', 'YYYY-MM-DD', true).isValid()
But is there a way to confirm that the format is correct? For example:
'YYYY-MM-DD' should return true, but
'YYYY-MM-DDsadsadl' should return false since the characters at the end of the string aren't valid DateTime chars.
We're working on a tool that allows a user to input an existing date format, and then a second input to enter the desired format, but we need validation to ensure the string can properly parse and convert, but they aren't entering a specific date.
The application must accept any and all possible date formats.
Use the following function to validate your format.
validFormat = function(inputFormat){
var validation = moment(moment('2017-06-17').format(inputFormat), inputFormat).inspect();
if(validation.indexOf('invalid') < 0)
return true;
else
return false;
}
Do spend some time to understand this. This simply does a reverse verification using inspect(). The date 2017-06-17 can be replaced by any valid date.
This Moment Js Docs will help you identify the valid formats.
Just make a call to this function as
validFormat('YYYY MM DD')
const getIsValid = inputFormat => moment(moment().format(inputFormat), inputFormat).isValid()
Explanation:
moment().format(inputFormat) - Create a date string from the current time from that format
This is then wrapped with moment() to make that string a moment date object, defining the format to parse it with. Finally we call the isValid() property on that moment date object. This ensures we are able to both create and parse a moment with our custom format.
How to get current format of the locale so that i can Format datetime with the format i get.
Means if i got the format then i can format that.
DateTime.Now.ToString(format);
This i am doing in visual studio with xamarin for creating Android application from C#.net.
Please help me for getting me the format.
Thanks & Regards
Parvez
You can use the culture info as formatter.
var d = DateTime.Now;
System.Diagnostics.Debug.WriteLine(
d.ToString(System.Globalization.CultureInfo.CurrentCulture));
System.Diagnostics.Debug.WriteLine(
d.ToString(System.Globalization.CultureInfo.CurrentUICulture));
try building the date format that you require, example below:
var now = DateTime.Now;
string newdateformat = string.Format("{0:00}/{1:00}/{2:0000}", now.Month, now.Day, now.Year);
If I understood your question correctly you want to format a DateTime to a string representation depending on the settings of the device (the locale).
You can do it like so:
var javaDate = new Date(YourDateTime.ToUnixTime()*1000);
var dt = DateFormat.DateTimeInstance;
SomeTextView.Text = string.Format("DateTime is: {0}", dt.Format(javaDate));
Notice that I used an extension method ToUnixTime() which converts DateTime to an Epoch time (also known as unix time) representation. You can find it easily even here on Stackoverflow.
(you can use DateFormat.DateInstance if you don't want the time part of the string)
Hope it helps.