Format Date in Android wear watchface - wear-os

I am trying to format the date in Android wear watchface as "dd-MMM" using this code
mDateFormat = DateFormat.getDateFormat(DigitalWatchFaceService.this);
mDateFormat.setCalendar(mCalendar);
mDateFormat.format("dd-MMM");
However, I get this error which is pointing to the last line of above code.
12-20 04:32:37.896 5616-5616/com.example.android.wearable.watchface
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.wearable.watchface, PID: 5616
java.lang.IllegalArgumentException: Cannot format given Object as a
Date
at java.text.DateFormat.format(DateFormat.java:302)
at java.text.Format.format(Format.java:157)
at
com.example.android.wearable.watchface.watchface.DigitalWatchFaceService$Engine.initFormats(DigitalWatchFaceService.java:290)
at
com.example.android.wearable.watchface.watchface.DigitalWatchFaceService$Engine.onCreate(DigitalWatchFaceService.java:235)
at
android.service.wallpaper.WallpaperService$Engine.attach(WallpaperService.java:875)
at
android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1166)
at
com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

I'd say the problem is that java.text.DateFormat.format() doesn't take a format string as a parameter - it takes a Date object. To specify a format string, either use android.text.format.DateFormat like this:
mDateFormat = DateFormat.getDateFormat(DigitalWatchFaceService.this);
String output = mDateFormat.format("dd-MMM", mCalendar);
or use java.text.SimpleDateFormat like this:
mDateFormat = new SimpleDateFormat("dd-MMM");
String output = mDateFormat.format(mCalendar.getTime());
[code was typed in here freehand, so not guaranteed bug-free!]

You can simply format it like this
mDateFormat = new SimpleDateFormat("dd-MMM");
Date mDate;
This will get it from the current context format:
mDateFormat = DateFormat.getDateFormat(DigitalWatchFaceService.this);
The full code is:
java.text.DateFormat mDateFormat;
mDateFormat = new SimpleDateFormat("dd-MMM");
private void initFormats() {
mDateFormat = new SimpleDateFormat("dd-MMM");
mDateFormat.setCalendar(mCalendar);
}
draw it somewhere:
canvas.drawText(
mDateFormat.format(mDate),
mXOffset, mYOffset + mLineHeight * (float)1.5, mDatePaint);
You need to call above in your onCreate:
#Override
public void onCreate(SurfaceHolder holder) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "onCreate");
}
super.onCreate(holder);
Resources resources = DigitalWatchFaceService.this.getResources();
mCalendar = Calendar.getInstance();
mDate = new Date();
initFormats();
}

Related

Unable to cast object of type ‘System.String’ to type ‘AuthBotES.ReturnIntents’

Currently I had integrate LUIS with Bot Framework v4.
When I search for result match with Intent,
the Bot return me with this Error:
Error : Unable to cast object of type ‘System.String’ to type ‘AuthBotES.ReturnIntents’.
My Source code as below:
if (stepContext.Result != null)
{
var result = (ReturnIntents)stepContext.Result;
var msg = $"{result}";
await stepContext.Context.SendActivityAsync(MessageFactory.Text(msg), cancellationToken);
}
and my ReturnIntents classes.
public class ReturnIntents
{
public string Intent { get; set; }
public double Score { get; set; }
public string Entities { get; set; }
}
A few issues here:
The first code block you posted looks to be for handling the result of a dialog, not for processing a LUIS result.
The cast from string to ReturnIntents will always fail.
Even if your cast of stepContext.Result to ReturnIntents did work, your msg variable would only contain namespace.to.class.ReturnIntents (the string representation of the object type, not the string representation of the objects properties.
Your msg variable is redundant.
I will address these in the order that they occur.
1 - Incorrect code block
This block of code looks suspiciously like code used to process a dialog and here e.g.:
var result = (bool)stepContext.Result;
Rather than the code for handling a LUIS result e.g.:
var dispatchResult = await cognitiveModels.DispatchService.RecognizeAsync(dc.Context, CancellationToken.None);
2 - Casting error
The error is telling you that it doesn't know how to convert a string object to a ReturnIntents object. To convert the string to your object you could use a couple of methods:
Use the NewtonSoft.Json NuGet package to allow you to turn the JSON string into your object as explained here.
2) A user defined type conversion as detailed in the official docs here and explained in this answer.
This error is a red herring in terms of your solution because I believe you're accidentally copied in the wrong block of code.
3 - Incorrect ToString behaviour
To get the string value of a ReturnIntents you will need to override the ToString method for the class and write your own custom implementation.
4 - Redundant cast
This:
// We know that this cast fails, and that stepContext.Result is a string
var result = (ReturnIntents)stepContext.Result;
// This will only return <namespace.path>.ReturnIntents (if the cast above works)
var msg = $"{result}";
// Passing in message msg isn't required, we can just pass in stepContext.Result
await stepContext.Context.SendActivityAsync(MessageFactory.Text(msg), cancellationToken);
Becomes:
var result = stepContext.Result;
await stepContext.Context.SendActivityAsync(MessageFactory.Text(result), cancellationToken);
So what I actually think you actually want is the following:
var dispatchResult = await cognitiveModels.DispatchService.RecognizeAsync<ReturnIntents>(dc.Context, CancellationToken.None);
Which will send the user's input off to LUIS, and deserialize the response into a ReturnIntents object.
Edit to provide solution to the OP
The ExecuteLuisQuery method called here, and defined here returns a ReturnIntents object.
This object is passed as an option to the ReturnIntentDialog here. Because this comes through as an instance of the object type you have a few options inside your FinalStepAsync method here to turn your options object into a ReturnIntents object.:
Casting
ReturnIntents returnIntents = null;
if (stepContext.Options is ReturnIntents)
{
returnIntents = (ReturnIntents)stepContext.Options;
}
Deserializing
using Newtonsoft.Json;
ReturnIntents returnIntents = null;
if (stepContext.Options is ReturnIntents)
{
returnIntents = JsonConvert.DeserializeObject<ReturnIntents>(JsonConvert.SerializeObject(stepContext.Options));
}

How do I get my hour, minutes, seconds, and milliseconds to be zeros?

I'm trying to get my format to be 2016-07-08T00:00:00.000Z.
String myDate = "20160708";
LocalDate myLocalDate = LocalDate.parse(myDate, DateTimeFormatter.BASIC_ISO_DATE);
OffsetDateTime myOffsetDate = myLocalDate.atTime(OffsetTime.now(ZoneOffset.UTC));
System.out.println(myOffsetDate); //2016-07-08T14:58:23.170Z
Well don't say "I want it to use the current time"! That's what this is doing:
OffsetTime.now(ZoneOffset.UTC)
If you just want an OffsetDateTime from a LocalDate by providing a zero offset and midnight, you can use:
OffsetDateTime myOffsetDate = myLocalDate
.atTime(LocalTime.MIDNIGHT)
.atOffset(ZoneOffset.UTC);
Or if you prefer:
OffsetDateTime myOffsetDate = myLocalDate
.atTime(OffsetTime.of(LocalTime.MIDNIGHT, ZoneOffset.UTC));
(Personally I prefer the first version, myself...)
Note that that's just getting you the right OffsetDateTime. If you want to format that with milliseconds and seconds, you'll need to do that explicitly:
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
System.out.println(myOffsetDate.format(formatter));
As noted in comments, if you're find with a ZonedDateTime instead of an OffsetDateTime, you can use
ZonedDateTime myOffsetDate = myLocalDate.atStartOfDay(ZoneOffset.UTC);
I am not quite sure if you can get the ISO_INSTANCE date format with the given string using LocalDate. but you can use below java 8 piece of code to get the required format.
public String getISO_8601DateFormat(String yyyyMMdd){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String requiredFormat = null;
try {
Date inputDate = sdf.parse(yyyyMMdd);
long dateLongRepresentation = inputDate.getTime();
long myTimeZoneOffset = TimeZone.getTimeZone(ZoneId.systemDefault()).getOffset(inputDate.getTime());
Instant instance = Instant.ofEpochMilli(dateLongRepresentation + myTimeZoneOffset);
requiredFormat = instance.toString();
} catch (ParseException e) {
e.printStackTrace();
}
return requiredFormat;
}
Enjoy coding with java :)

How to add link in atom feed using spring 3, netbeans

Using spring3.2 and netbeans 7.2
Warning code :
#Override
protected List<Entry> buildFeedEntries(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception {
#SuppressWarnings("unchecked")
List<Feed_vo> contentList = (List<Feed_vo>) model.get("feedContent");
List<Entry> entries = new ArrayList<Entry>(contentList.size());
for (Feed_vo content : contentList) {
Entry entry = new Entry();
String date = String.format("%1$tY-%1$tm-%1$td", content.getCreatedDate());
entry.setId(String.format("tag:featuriz.com,%s:%d", date, content.getId()));
entry.setTitle(String.format("%s | on %s by %s",content.getTitle(), date, content.getAuthor()));
entry = setLink(content, entry);
entry.setUpdated(content.getCreatedDate());
Content summary = new Content();
summary.setValue(content.getSummary());
entry.setSummary(summary);
entries.add(entry);
}
return entries;
}
private Entry setLink(Feed_vo vo, Entry entry) {
ArrayList l = new ArrayList();
Link link = new Link();
link.setType("text/html");
link.setHref(vo.getUrl());
l.add(link);
entry.setAlternateLinks(l);
return entry;
}
This code works but Netbeans warning :
/home/sudhakar/**/CustomAtomViewer.java:72: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
l.add(link);
1 warning
How to solve this warning.
Also inform me the correct format for atom and rss feed.
(How the output should look like. ie. output source).
Warnings solved by this code:
List<Link> v = new ArrayList<Link>();
Link link = new Link();
link.setType("text/html");
link.setHref(vo.getUrl());
v.add(link);
entry.setAlternateLinks(v);

Protobuf C# Message Translation to JAVA

I am trying to translate a message generated using C# to JAVA. As a first step, i generated proto file and this is what i got
package Om.Business.Scanner;
message ScannerActivityDetail {
optional string ActivityId = 1;
optional string ContextId = 2;
optional int32 ActivityStart = 3;
optional bcl.DateTime ActivityEnd = 4;
}
How do i interpret bcl.DateTime in java world?
I am using protobuf-net and trying to de-serialize message generated by C# app.
Thanks in advance for your help.
Looking at bcl.proto, it should be pretty straightforward. Create a Map<DateTime.TimeSpanScale, TimeUnit> in the obvious way, then:
public static Date toDate(bcl.DateTime proto) {
TimeUnit unit = SCALE_TO_UNIT_MAP.get(proto.getScale());
if (unit == null) {
throw new IllegalArgumentException("Invalid scale: " + proto.getScale());
}
long millis = unit.toMillis(proto.getValue());
return new Date(millis);
}
You could use Joda Time's DateTime type in exactly the same way, as it has a constructor accepting a long too. (You might want to think about which time zone to specify though...)

How do I read the Received Date from Outlook MSG files -without- the Outlook API?

I need to read stuff from an Outlook msg file. Currently I'm using a class from CodeProject.com project to accomplish this, since deploying VSTO and Outlook on a server is not an option.
This class gets To, From, CC, Subject, Body, and everything else I need from the msg file, except Date information (such as Received Date and Sent Date).
There is some (really, really low-level) documentation on how to get stuff out of msg files on MSDN, but it's a little beyond the scope of this project and doesn't mention dates at all.
Ideally I'd be able to have a drop-in replacement for the class I am using now (OutlookStorage.cs in the previously mentioned CodeProject) or be able to modify the existing class a bit. To modify, I would need the correct 4 character hexidecimal prop identifier for received date. For instance, Subject is listed as PR_SUBJECT = "0037" and Body is listed as PR_BOY = "1000".
If you're using OutlookStorage.cs from CodeProject, then add the following:
private const string PR_RECEIVED_DATE="007D";
private const string PR_RECEIVED_DATE_2 = "0047";
...
/// <summary>
/// Gets the date the message was received.
/// </summary>
public DateTime ReceivedDate
{
get
{
if (_dateRevieved == DateTime.MinValue)
{
string dateMess = this.GetMapiPropertyString(OutlookStorage.PR_RECEIVED_DATE);
if (String.IsNullOrEmpty(dateMess))
{
dateMess = this.GetMapiPropertyString(OutlookStorage.PR_RECEIVED_DATE_2);
}
_dateRevieved = ExtractDate(dateMess);
}
return _dateRevieved;
//return ExtractDate(dateMess);
}
}
private DateTime _dateRevieved = DateTime.MinValue;
private DateTime ExtractDate(string dateMess)
{
string matchStr = "Date:";
string[] lines = dateMess.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
if (line.StartsWith(matchStr))
{
string dateStr = line.Substring(matchStr.Length);
DateTime response;
if (DateTime.TryParse(dateStr, out response))
{
return response;
}
}
}
return DateTime.MinValue;
}
I think the Aspose library will do what you want, ok it a 3rd party lib so may not be what you want. There are a few vbs scripts around that get basic infomation out of msg files that could be translated.
Got a hint from this:
string fullFileName = "c:\message.msg";
DateTime dateRevieved = new DateTime();
StreamReader sr = new StreamReader(fullFileName, Encoding.Default);
string full = sr.ReadToEnd();
string date;
int iStart;
int iLast;
string caption;
//This -should- handle all manner of screwage
//The ONLY way it would not is if someone guessed the -exact- to-the-second
//time that they send the message, put it in their subject in the right format
while (true) { //not an infinite loop, I swear!
caption = "Date:";
if (full.IndexOf("Date:") > -1) { //full shortens with each date is removed
string temp = "";
iStart = full.LastIndexOf(caption);
temp = full.Remove(0, iStart + caption.Length);
full = full.Substring(0, iStart);
iLast = temp.IndexOf("\r\n");
if (iLast < 0) {
date = temp;
} else {
date = temp.Substring(0, iLast);
}
date = date.Trim();
if (date.Contains(subject) || subject.Contains(date)) {
continue; //would only happen if someone is trying to screw me
}
try {
dateRevieved = DateTime.Parse(date); //will fail if not a date
break; //if not a date breaks out of while loop
} catch {
continue; //try with a smaller subset of the msg
}
} else {
break;
}
}
This is kind of a hack compared to the ways you can get other things from msg files using something this lovely project. Still, it's stood up to everything I have thrown against it, and as noted the -only- way to fool it is to put the exact to-the-second date in the subject line in the proper format.
to combine your two posts I would suggest the following solution:
To modify, I would need the correct 4 character hexidecimal prop identifier for recieved date. For instance, Subject is listed as PR_SUBJECT = "0037" and Body is listed as PR_BOY = "1000".
Look for "007D".
Use the method you posted in your second post on the received data to eliminate the problem when the same (date) string is inside the subject.
I have to mention that this method doesn't seem to work on internal eMails: In mails I receive from colleagues, there is no substg1.0_007Dxxxx-Property.
Here, the date seems to be hidden in substg1.0_0047xxxx.
All the best!
inno

Resources