我正在尝试将格式化程序添加到我的Automapper配置中以设置所有DateTime?字段的样式.我尝试过全局添加格式化程序:
Mapper.AddFormatter<DateStringFormatter>();
Run Code Online (Sandbox Code Playgroud)
并在具体的映射本身:
Mapper.CreateMap<Post, PostViewModel>()
.ForMember(dto => dto.Published, opt => opt.AddFormatter<DateStringFormatter>());
Run Code Online (Sandbox Code Playgroud)
但似乎都不起作用 - 它总是以正常格式输出日期.作为参考,这里是我正在使用的ViewModel,以及其余的配置:
public class DateStringFormatter : BaseFormatter<DateTime?>
{
protected override string FormatValueCore(DateTime? value)
{
return value.Value.ToString("d");
}
}
public abstract class BaseFormatter<T> : IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
if (context.SourceValue == null)
return null;
if (!(context.SourceValue is T))
return context.SourceValue == null ? String.Empty : context.SourceValue.ToString();
return FormatValueCore((T)context.SourceValue);
}
protected abstract string FormatValueCore(T value);
}
Run Code Online (Sandbox Code Playgroud)
PostViewModel:
public int PostID { get; set; }
public …Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的时间字符串:5:34 pm.我想抓住它5,把它放在一个变量中,抓住它34并把它放在一个变量中.这可能吗?
提前致谢!
我正在尝试在C#中为学校项目制作一个客户端 - 服务器应用程序我的问题是一个类被序列化好并通过套接字发送,而另一个不是,我无法弄明白.
Employee类(以及Bonus)正在被序列化,但是当我尝试将一个Transfer实例传递给formater时
formatter.Serialize(stream, transferObj);
Run Code Online (Sandbox Code Playgroud)
它抛出异常:带有消息的NotSupportedException:内存流不可扩展.
sendToServer()
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse("127.0.0.1");
System.Net.IPEndPoint remoteEP = new IPEndPoint(ipAdd, 6666);
socket.Connect("127.0.0.1", 6666);
Transfer t = new Transfer();
Employee e = new Employee();
Bonus b = new Bonus(); b.setAmmount(234); b.setDescription("xxxx");
e.getBonuses().Add(b);
byte[] buffer = new byte[1000];
System.Console.WriteLine("client started");
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream(buffer);
formatter.Serialize(stream, e);
// Employee and Bonus are serialized but not Transfer
stream.Flush();
socket.Send(buffer,buffer.Length,0);
Run Code Online (Sandbox Code Playgroud)
员工类
using System;
using System.Collections.Generic; …Run Code Online (Sandbox Code Playgroud) 假设您有一些表格数据,其中数据长度可能会有所不同,是否有一个使用Formatter类来自动调整填充?
所以不要这样(注意列A):
columnA columnB
1 34.34
10 34.34
100 34.34
1000 34.34
Run Code Online (Sandbox Code Playgroud)
你得到这个(注意B栏):
columnA columnB
1 34.34
10 34.34
100 34.34
1000 34.34
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试过这只包含%5s%5s之间的空格,但它们是静态的,不会调整以产生我想要的结果.我认为5%的5s会自动填充5个空格
Formatter formatter = new Formatter();
formatter.format("%5s %5s", columnAdata, columnBdata);
Run Code Online (Sandbox Code Playgroud) 我只是无法弄清楚如何设置NSDateFormatter日期格式,因此[formatter dateWithString:]不返回nil.
日期如下:
2008-08-18T14:32:22.1875000 + 01:00
我试过了
formatter setDateFormat:@"yyyy-MM-ddTHH:mm:ss.fffffffzz+GMT"];
Run Code Online (Sandbox Code Playgroud)
似乎没有工作:(.任何建议,请帮助.
好的,谢谢你的回答,似乎我误解了我的问题,所以我试着改写它,对不起,也许它对编程来说有点迟了.我需要一个日期包装器对象,所以我不必自己解析包含日期的字符串.基本上我从数据库中获取一个我需要解析的字符串,我可以在我的UI中使用它.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZ";
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDate *date = [formatter dateFromString:@"2008-08-18T14:32:22.1875000+01:00"];
NSLog(@"%@", date);
Run Code Online (Sandbox Code Playgroud)
打印为零
我想要的是NSString-> NSDat,所以我可以问一下它包含的月份或它包含的时间.我现在看到NSDate没有这样的方法.这是一个愚蠢的假设.现在有什么想法吗?
我的服务器代码将一些列作为比率值传输,0.0到1.0.我需要格式化并以百分比形式编辑它.我想在JavaScript方面这样做,而无需修改服务器端.因此,如果我添加一个自定义格式化程序,只是将该值乘以100,则显示按预期工作.此外,当我点击编辑按钮时,内联编辑框也会以百分比形式显示值.麻烦在我保存时开始 - 这个值再次用格式化器转换,给我10000个东西.好吧,我需要对称,所以我创建了一个unformatter,它只是将值除以100.但这也不行 - 现在编辑控件将其显示为不是我想要的比例(虽然保存现在可以正常工作).
有没有改变服务器代码的方法?
我正在尝试使用set_major_formatter在matplotlib图上格式化yaxis。该图已正确生成,但ax.yaxis.set_major_formatter()引发了一些奇怪的错误。
格式化程序:
def mjrFormatter(x):
return "{0:.0f}%".format(x * 100)
Run Code Online (Sandbox Code Playgroud)
使用格式化程序的代码:
...
ax.yaxis.set_major_formatter(mjrFormatter)
...
Run Code Online (Sandbox Code Playgroud)
错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-108-b436fe657a8b> in <module>()
----> 1 plot_func(data = data, figsize=(20,10), fig_title = 'title')
<ipython-input-107-d60ffc010a75> in plot_percent_moc(data, figsize, fig_title)
16 _ = data2[col].plot()
17
---> 18 ax.yaxis.set_major_formatter(mjrFormatter)
19
20 fig.suptitle(fig_title, fontsize = 14)
C:\Python27\lib\site-packages\matplotlib\axis.pyc in set_major_formatter(self, formatter)
1396 self.isDefault_majfmt = False
1397 self.major.formatter = formatter
-> 1398 formatter.set_axis(self)
1399
1400 def set_minor_formatter(self, formatter):
AttributeError: 'function' object has no attribute 'set_axis'
---------------------------------------------------------------------------
AttributeError …Run Code Online (Sandbox Code Playgroud) 正如我们所知,默认情况下,Web API框架具有可以生成JSON或XML的格式化程序如果您想生成其他类型的输出,那么在很多地方我已经读过它需要实现自定义媒体类型格式化程序,它会做你做的事情想.但是我需要从我的Web API控制器返回原始HTML,如果我以这种方式创建HttpResponseMessage:
response = new HttpResponseMessage()
{
Content = new StringContent(
"<p>Hello world</p>",
Encoding.UTF8,
"text/html")
};
Run Code Online (Sandbox Code Playgroud)
然后我在我的响应中有原始HTML,浏览器将识别并呈现.我的问题实际上为什么它没有使用任何自定义格式化程序,并且在这种情况下我需要创建一个,我不应该.
格式化程序只在关闭流时才会写出来吗?
码:
public void save(String content, String filename) {
if(filename!=""){
setFileName(filename);
try {
output = new Formatter(new File(fileName));
output.format("%s",content);
} catch (FormatterClosedException ex){
System.err.println("Error writing to file");
return;
} catch ( NoSuchElementException ex){
System.err.println("invalid input");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
output.close();
}
Run Code Online (Sandbox Code Playgroud)
起初我忘了用output.close()添加finally块,当调用该方法时,它不会写任何东西.那么格式化程序在关闭流时只实际写入其内容是否正确?
我正在尝试使用类似的代码解析Joda-Time的时间(从使用Joda Date&Time API的 btiernay复制来解析多种格式)
DateTimeParser[] parsers = {
DateTimeFormat.forPattern( "yyyy-MM-dd HH" ).getParser(),
DateTimeFormat.forPattern( "yyyy-MM-dd" ).getParser() };
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append( null, parser).toFormatter();
DateTime date1 = formatter.parseDateTime( "2010-01-01" );
DateTime date2 = formatter.parseDateTime( "2010-01-01 01" );
Run Code Online (Sandbox Code Playgroud)
但是,该应用程序可能在海外使用,这意味着该月可能包含外语,
例如6月土耳其语的Haz缩短了,如何解析"07.Haz.2014",它抛出java.lang.IllegalArgumentException:格式无效
formatter ×10
java ×3
asp.net ×1
automapper ×1
c# ×1
date ×1
datetime ×1
int ×1
ios ×1
iphone ×1
jqgrid ×1
matplotlib ×1
objective-c ×1
parsing ×1
percentage ×1
sockets ×1
stream ×1